Skip to content

Challenge 1: Get the First Set Bit Position Using the Right Shift

This problem is similar to the last lesson we discussed. If you need a clue, return to the previous lesson to further your understanding.

Gopi Gorantala
Gopi Gorantala
1 min read

This problem is similar to the last lesson we discussed. If you need a clue, return to the previous lesson to further your understanding.

Introduction

In this question, we need to find the first set-bit position from the right.

Problem statement

Given an input number, find the first set-bit position of the number.

Input: n = 18 

Output: 2

Coding exercise

This problem is designed for your practice, so try to solve it yourself first. You can always refer to the solution in the next lesson if you get stuck. Good luck!

Hint: Use the previous logic we discussed to solve this.
// java
// TODO: finish the challenge or check next lesson for solution
class Solution {
    public static int getFirstSetBitPos(int n) {
        // Write - Your - Code- Here
        
        return -1; // change this and return the position of the first set-bit.
    }
}
# Python
# TODO: finish the challenge or check next lesson for solution
def getFirstSetBitPos(n):
	# Write - Your - Code- Here
        
    return -1 # change this and return the position of the first set-bit.
// javascript
// TODO: finish the challenge or check next lesson for solution
const getFirstSetBitPos = n => {
    // Write - Your - Code- Here

    return -1; // change this and return the position of the first set-bit.
}
// javascript
// TODO: finish the challenge or check next lesson for solution
#include <iostream>
using namespace std;

int getFirstSetbit(int n) {
  // Write - Your - Code- Here

  return -1; // change this and return the position of the first set-bit.
}
// typescript
// TODO: finish the challenge or check next lesson for solution
export const getFirstSetBitPos = (n: number): number => {
    // Write - Your - Code- Here

    return -1; // change this and return the position of the first set-bit.
}

The solution will be explained in the next lesson.

Data Structures and AlgorithmsBit Manipulation

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things