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;
}

- Vir Pratap Uttam April 30, 2015 | Flag Reply
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

- AD January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@AD:
i = i >> 1 right? Unlike you wrote ">>>".

- aka January 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Murali Mohan January 29, 2014 | Flag
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.

- whatevva' February 04, 2014 | Flag
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;

- Coder January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

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

- ivy January 29, 2014 | Flag Reply
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.

- Anonymous January 28, 2014 | Flag Reply
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

- andrej January 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it returns -1

- Anonymous January 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous January 28, 2014 | Flag Reply
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 }

- Anonymous January 28, 2014 | Flag Reply
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)

- Jeff January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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)

- jeff January 28, 2014 | Flag
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)

- Anonymous January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this?

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

- Jason January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Alin January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your code gave me an idea to make a new version

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

- Jeff January 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

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

- Anonymous January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

AD's answer is perfect. Here is a bit of explanation:

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)

- Mocoder January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

career cup app for android

- https://play.google.com/store/apps/details?id=com.andersoncouncil.careercup.ui January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int int_max = -1 >> 1

- ivy January 29, 2014 | Flag Reply
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++;
        }
    }

}

- RG January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- JS February 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- feliciafay February 05, 2014 | Flag Reply
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;
}

- Bhuvan February 07, 2014 | Flag Reply
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)

- abhityagi85 June 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- vathsala September 11, 2014 | Flag Reply
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

- Fran├žois Birot December 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
- Anonymous May 31, 2015 | Flag Reply
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;
}

- Dharma May 31, 2015 | Flag Reply
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;
}

- x May 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- x May 31, 2015 | Flag
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.

- Kaushal August 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java: -(1<<31)-1

- naveen December 09, 2015 | Flag Reply
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));

- kaz April 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- swapnilsj August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>

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

- alakesh.haloi January 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>

int add(int i, int j)
{
  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)
    {
      result = add(result, i);
    }
    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));
    i = add(i, 1);
    ret <<= 1;
    ret |= 1;
  }
}

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

- Qinggang Bian January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- abhityagi85 June 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int int_max = -1 >> 1

- ivy January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- vathsala September 11, 2014 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More