## Google Interview Question for Software Engineer / Developers

Country: United States

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

private int maxInt()
{
int a=-1;
a=a>>>1;
return a;
}

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

int i = -1;
i = i >>> 1;

//This will shift the singed bit to right by 1 and hence making the value 2147483647

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

i = i >> 1 right? Unlike you wrote ">>>".

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

We need logical right shift here, so >>> is correct. Otherwise, the arithmetic right shift >> would always yield -1.

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

This solution is acceptable in Java since Java supports the logical shift right ">>>" operation. However, this wouldn't work in C as it doesn't support this operation. Infact, a common interview question is to implement logical shift right operation in C.

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

The best way to get the max value of int on 32 or 64 bit processors is by negating the 0(so that we get all 1's) and then doing an unsigned right shift (>>> in java).

int c = (~0) >>> 1;

In C, if we do normal shift (~0 >> 1), the compiler will perform arithmetic shift and will append the sign bit into the shifted bit. Instead, we would have to force a logical shift by explicitly declaring the value as unsigned, i.e. by doing something like this:

~0u >> 1;

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

int int_max = (unsigned int)-1 >> 1;

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

int i=0;
int int_max = ~i & (~i-1) //all ones and-ed with sign bit 0 rest all ones.

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

doesn't work for me but:

int int_max = (unsigned int) (~0) >> 1

does

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

it returns -1

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

int m = ~(1<<(sizeof(int)*8-1))

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

int m = (~0u)>>1;

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

``````1 #include<stdio.h>
2
3 int main()
4 {
5   unsigned int i=0;
6   printf("%u\n" ,((unsigned int)~i/2));
7   return 0;
8 }``````

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

def max_int():
import struct
max_int_list = [1] * (struct.calcsize("i")*8)
max_int_list[0] = 0 # highest priorty bit is reserved for negative flag
max_int_str = "".join(str(x) for x in max_int_list)
print(max_int_str)
return int(max_int_str,2)

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

opps the struct type should be "P" to get the system bit size

``````def max_int():
import struct
max_int_list = [1] * (struct.calcsize("P")*8)
max_int_list[0] = 0
max_int_str = "".join(str(x) for x in max_int_list)
print(max_int_str)
return int(max_int_str,2)``````

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

``````def max_int():
import struct
max_int_list = [1] * (struct.calcsize("i")*8)
max_int_list[0] = 0 # highest priorty bit is reserved for negative flag
max_int_str = "".join(str(x) for x in max_int_list)
print(max_int_str)
return int(max_int_str,2)``````

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

``````cout << "MY_INT_MAX = " << ((-1U) >> 1) << endl;
cout << "MY_LONG_MAX = " << ((-1UL) >> 1)  << endl;``````

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

int MAX_INT = ~0 & ~(1 << 31);

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

your code gave me an idea to make a new version

``````def max_int2():
import struct
return((1 << (struct.calcsize("P")*8-1))-1)``````

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

#include<stdio.h>

int main(){
printf("%u\n",((unsigned int)~0));
}

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

Imagine you have a signed 4 bit system. In that system

``-1``

would be this:

``````1111
||_ from here bits are inverted
|_ sign bit (negative)``````

Shift to right add a zero to beggining of sequence:

``````0111
||_ regular bits. all 1 from here (max value)
|_ sign bit (positive)``````

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

career cup app for android

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

int int_max = -1 >> 1

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

``````public class GenMaxInt {
public static void main(String[] args) {
int i = 0;
int j =0;
int ans =0;
while(true) {
int y = ~i& 1 << j;
if( y < 0) {
System.out.println(ans);
break;
}
ans += y;
j++;
}
}

}``````

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

printf("MAX_INT=%d\n", ~(1<<31));

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

//C++
int int_max=~(1<<31);
int int_min=1<<31;

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

``````// Generate INT_MAX
void int_max_generate()
{
int num = 1;
num = static_cast<unsigned int>(-1) >> 1; // instead of -1 use ~0
cout << num << endl;
}``````

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

``````#define MY_MAX_INT(x) ((~(x & 0)) & ~(((x & 0)|1) << (sizeof(x)*8-1)))

int x = MY_MAX_INT(x);
long long y = MY_MAX_INT(y)``````

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

#define int_max(TYPE) ( (1<< ((sizeof(TYPE)*8)-1)) -1)

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

This is how I would do:

int max = (unsigned int) -1 >> 1;

You could also replace -1 by ~0

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

``````#include <limits.h>
#include <stdio.h>

int main()
{
/* find the position of the most significant bit */
int msb_pos = (sizeof(int) * CHAR_BIT) - 1;

/* Move number 1 to the MSB pos and then take xor with -1 */
int result = (1 << msb_pos) ^ -1;

printf("Result = %d\n", result);
printf("Builtin = %d\n", INT_MAX);

return result;
}``````

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

I read in a book that the effect of >> on a signed int is undefined. That is, different compilers generated different types of code. Thus, I can up with this code.

``````#include <limits.h>
#include <stdio.h>

int main()
{
/* find the most significant bit (MSB) pos */
int msb_pos = (sizeof(int) * CHAR_BIT) - 1;

/* mov number 1 to the MSB pos and then xor with -1 */
int result = (1 << msb_pos) ^ -1;

printf("Result = %d\n", result);
printf("Builtin = %d\n", INT_MAX);

return result;
}``````

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

minor comment: main is not a good function name. I was using it for just running my test.

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

C'mon guys 32 bit processor does not mean necessarily mean that size of int is 4 bytes.

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

Java: -(1<<31)-1

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

``````int max_int = ~0;
size_t size = sizeof(int);
max_int = max_int & ~(1 << ((size * 8) - 1));``````

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

``````int  i = -1;
i = (unsigned int) i >> 1;
cout << i << endl;``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>

int main(){
printf("%u\n",((unsigned int)~0));
}

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````#include <stdio.h>

{
int sum = i ^ j;
int carry = i & j;
while(carry != 0){
carry = carry << 1;
i = sum;
j = carry;
sum = i ^ j;
carry = i & j;
}
return sum;
}

int multiply(int i, int j){
int result = 0;
while(j != 0)
{
if(j & 01)
{
}
i <<= 1;
j >>= 1;
}
return result;
}

int get()
{
int size = sizeof(int);
unsigned int ret = 1;
int i = 1;

while(true){
if(i == 31)
return (int)(ret | (1 << size));
ret <<= 1;
ret |= 1;
}
}

int main()
{
printf("%d\n", get());
}``````

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

looks like you have used all the programming you know. Cheers

Comment hidden because of low score. Click to expand.
-1
of 1 vote

int int_max = -1 >> 1

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

#define int_max(TYPE) ( (1<< ((sizeof(TYPE)*8)-1)) -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.