## InMobi Interview Question for Senior Software Development Engineers

Country: India
Interview Type: Written Test

Comment hidden because of low score. Click to expand.
0
of 0 vote

// Java program to Count set
// bits in an integer
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

class SpecalNumberCount
{
//finding specal numbers
public static int countSetBits(int firstNumber, int secondNumber)
{
ArrayList<Integer> specalSetnumber= new ArrayList<Integer>();
while (firstNumber <= secondNumber) {
if (allsetBit(firstNumber) || setBitFollowedByUnsetBits(firstNumber) || areSetBitsIncreasing(firstNumber)){
//All set specal no.
//System.out.println("specalSetnumber "+ firstNumber);
}
firstNumber ++;
}
return specalSetnumber.size();
}
//b) case
private static boolean setBitFollowedByUnsetBits(int num) {
if (num == 0) return false;
if ((num & 1) == 1) return false;
int invK = ~ (num | (num-1));
int indicator = invK | (invK-1);
return indicator == 0xffffffff;
}
// find all set bit in a number --> a) case
public static boolean allsetBit(int number)
{
int counter = 0;
while(number>0){
if(number%2 == 1){
counter++;
} else {
return false;
}
number = number/2; //or number = number >> 1
}
return true;
}

// c) case
public static boolean areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = 999999;

// We traverse bits from right to left
// and check if counts are decreasing
// order.
while (n > 0)
{
// Ignore 0s until we reach a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;

// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}

// Compare current with previous and
// update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}

return true;
}
// Main start
public static void main(String args[])
{
Scanner dd = new Scanner(System.in);
int[] vars = new int[dd.nextInt()*2];

for(int i = 0; i < vars.length; i++) {
vars[i] = dd.nextInt();
}
for (int j = 0; j < vars.length; j=j+2) {
System.out.println(countSetBits(vars[j], vars[j+1]));
}
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

// Java program to Count set
// bits in an integer
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

class SpecalNumberCount
{
//finding specal numbers
public static int countSetBits(int firstNumber, int secondNumber)
{
ArrayList<Integer> specalSetnumber= new ArrayList<Integer>();
while (firstNumber <= secondNumber) {
if (allsetBit(firstNumber) || setBitFollowedByUnsetBits(firstNumber) || areSetBitsIncreasing(firstNumber)){
//All set specal no.
//System.out.println("specalSetnumber "+ firstNumber);
}
firstNumber ++;
}
return specalSetnumber.size();
}
//b) case
private static boolean setBitFollowedByUnsetBits(int num) {
if (num == 0) return false;
if ((num & 1) == 1) return false;
int invK = ~ (num | (num-1));
int indicator = invK | (invK-1);
return indicator == 0xffffffff;
}
// find all set bit in a number --> a) case
public static boolean allsetBit(int number)
{
int counter = 0;
while(number>0){
if(number%2 == 1){
counter++;
} else {
return false;
}
number = number/2; //or number = number >> 1
}
return true;
}

// c) case
public static boolean areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = 999999;

// We traverse bits from right to left
// and check if counts are decreasing
// order.
while (n > 0)
{
// Ignore 0s until we reach a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;

// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}

// Compare current with previous and
// update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}

return true;
}
// Main start
public static void main(String args[])
{
Scanner dd = new Scanner(System.in);
int[] vars = new int[dd.nextInt()*2];

for(int i = 0; i < vars.length; i++) {
vars[i] = dd.nextInt();
}
for (int j = 0; j < vars.length; j=j+2) {
System.out.println(countSetBits(vars[j], vars[j+1]));
}
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

// Java program to Count set
// bits in an integer
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

class SpecalNumberCount
{
//finding specal numbers
private static int countSetBits(int firstNumber, int secondNumber)
{
ArrayList<Integer> specalSetnumber= new ArrayList<Integer>();
while (firstNumber <= secondNumber) {
if (allsetBit(firstNumber)){
//All set specal no.
//System.out.println("specalSetnumber "+ firstNumber);
} else if (setBitFollowedByUnsetBits(firstNumber)){
} else if (areSetBitsIncreasing(firstNumber)){
}
firstNumber ++;
}
return specalSetnumber.size();
}
//b) case
private static boolean setBitFollowedByUnsetBits(int num) {
if (num == 0) return false;
if ((num & 1) == 1) return false;
int invK = ~ (num | (num-1));
int indicator = invK | (invK-1);
return indicator == 0xffffffff;
}
// find all set bit in a number --> a) case
private static boolean allsetBit(int number)
{
while(number>0){
if(number%2 != 1){
return false;
}
number = number/2; //or number = number >> 1
}
return true;
}

// c) case
private static boolean areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = 999999; //Max

// We traverse bits from right to left
// and check if counts are decreasing
// order.
while (n > 0)
{
// Ignore 0s until we reach a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;

// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}

// Compare current with previous and
// update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}

return true;
}
// Main start
public static void main(String args[])
{
Scanner dd = new Scanner(System.in);
int[] vars = new int[dd.nextInt()*2];

for(int i = 0; i < vars.length; i++) {
vars[i] = dd.nextInt();
}
for (int j = 0; j < vars.length; j=j+2) {
System.out.println(countSetBits(vars[j], vars[j+1]));
}
}
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````// Java program to Count set
// bits in an integer
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

class SpecalNumberCount
{
//finding specal numbers
private static int countSetBits(int firstNumber, int secondNumber)
{
ArrayList<Integer> specalSetnumber= new ArrayList<Integer>();
while (firstNumber <= secondNumber) {
if (allsetBit(firstNumber)){
//All set specal no.
//System.out.println("specalSetnumber "+ firstNumber);
} else if (setBitFollowedByUnsetBits(firstNumber)){
} else if (areSetBitsIncreasing(firstNumber)){
}
firstNumber ++;
}
return specalSetnumber.size();
}
//b) case
private static boolean setBitFollowedByUnsetBits(int num) {
if (num == 0) return false;
if ((num & 1) == 1) return false;
int invK = ~ (num | (num-1));
int indicator = invK | (invK-1);
return indicator == 0xffffffff;
}
// find all set bit in a number --> a) case
private static boolean allsetBit(int number)
{
while(number>0){
if(number%2 != 1){
return false;
}
number = number/2; //or number = number >> 1
}
return true;
}

// c) case
private static boolean areSetBitsIncreasing(int n)
{
// Initialize previous count
int prev_count = 999999; //Max

// We traverse bits from right to left
// and check if counts are decreasing
// order.
while (n > 0)
{
// Ignore 0s until we reach a set bit.
while (n > 0 && n % 2 == 0)
n = n/2;

// Count current set bits
int curr_count = 1;
while (n > 0 && n % 2 == 1)
{
n = n/2;
curr_count++;
}

// Compare current with previous and
// update previous.
if (curr_count >= prev_count)
return false;
prev_count = curr_count;
}

return true;
}
// Main start
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int[] vars = new int[scanner.nextInt()*2];

for(int i = 0; i < vars.length; i++) {
vars[i] = scanner.nextInt();
}
scanner.close();
for (int j = 0; j < vars.length; j=j+2) {
System.out.println(countSetBits(vars[j], vars[j+1]));
}
}
}``````

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.