This is the transcript of the mock interview with Candidate PS (1 year)

**SDEGuru:** Welcome to the IM interview. We will spend the next 30 minutes discussing few interview questions. Do you have any questions before we get started?

**Candidate PS:** I am ready. Let’s go!

**SDEGuru:** Cool, What is your favorite programming language?

**Candidate PS:** C++

**SDEGuru:** Ok! How do you rate yourself in C++? On a scale of 1 to 10 with 1=worst and best=10?

**Candidate PS:** I would rate myself 6/10.

**SDEGuru:** I see, What will it take to get you from 6/10 to 7/10 in C++

**Candidate PS:** Mastering STL, C++11 and OOP. Actually, C++ is becoming more powerful these days thanks to these

**SDEGuru:** I see, do you know what is the “diamond problem”?

**Candidate PS:** No.

**SDEGuru:** What is the difference between `++i`

and `i++`

**Candidate PS:** I can explain with an example. if we have `ans1 = ++i`

and `ans2 == i++`

**Candidate PS:** When i is 4, ans1 will be 5 and ans2 will be 4. This is because ++ has taken precedence in first case and is incremented before storing in ans1

**SDEGuru:** Makes sense, I have seen code lines that simpley say “`++i;`

” or “`i++;`

” in a line all by itself. Is there any difference there?

**Candidate PS:** No, there shouldn’t be any difference. `++i`

translates to `i = i+1`

, and `i++ to i = 1+i`

**SDEGuru:** Next question, what do you mean by ternary operator?

**Candidate PS:** `a = (expr?) true : false`

. This is the ternary operator

**SDEGuru:** Is that “the” ternary operator? or is that “a” ternary operator?

**Candidate PS:** ternary is one which can handle 3 inputs.

++ is unary and a + b is binary

**SDEGuru:** Good, can you name two more unary operators

**Candidate PS:** Well how about decrement (–)

**SDEGuru:** Good, one more.

**Candidate PS:** I dont know

**SDEGuru:** Take some time and hink a bit

**Candidate PS:** I got it! XOR, NOT

**SDEGuru:** XOR is binary, and yes NOT is unary.

**SDEGuru:** Good, now lets us get into a coding question shall we?

**Candidate PS:** sure.

**SDEGuru:** Given a 64-bit long number, can you count the number of bits that are set to 1?

**Candidate PS:** Alright, I would XOR every bit with 1 and right shift every bit

**SDEGuru:** I am not sure if I follow your algorithm, let us write the code up first.

**Candidate PS:** sure

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include #include using namespace std; // Given a “long” number, can you count the number of bits that are set to 1? // To execute C++, please define "int main()" int findNumBitSet(long input) { int setBits = 0; while (input) { setBits += (input & 1); input = input>>1; } cout << "The number of setBits in " << input << " " << setBits << endl; return -1; } int main() { long input1 = 16; // answer = 1 long input2 = -1; // answer = 4 long input3 = LONG_MIN; //answer = 1 long input4 = LONG_MAX; //answer = 63 findNumBitSet(input1); findNumBitSet(input2); findNumBitSet(input3); findNumBitSet(input4); } |

**Candidate PS:** I am not sure if this is right. I am going to test it.

**Candidate PS:** Oops, Time Limit Exceeded.

**Candidate PS:** Fixed some bugs, Oops, erroneous output

**Candidate PS:** I figured out what is going wrong. I used the wrong bitwise. I should use AND becuase I need only a ON or OFF

**SDEGuru:** Is it correct now?

**Candidate PS:** Developer in me says yes, but tester says no

**SDEGuru:** ok, let me know when the tester in your is satisfied

**Candidate PS:** Need to test more edge cases

Ok, I think I am right. Please have a look.

**SDEGuru:** Why does the output say “The number of setBits in 0 19 ?”

**Candidate PS:** It is because every input is reduced to zero. My bad

**SDEGuru:** Will your code work for -ve numbers?

**Candidate PS:** Yes, I want to test for 0xFFFF. Which is essentially -1.

**SDEGuru:** Clearly, when we put in the values of -1 and 0xffff the output is different, so they are not the same.

**Candidate PS:** Yes, it is 32 bits I thought

**SDEGuru:** Let us use LONG_MAX and LONG_MIN

**SDEGuru:** What is going on? Your output for LONG_MAX is “-1 is 63 bits”. Why?

**Candidate PS:** Wait, another bug!

**SDEGuru:** I expect -1 to say 64 bits, and LONG_MIN to say 1 right? It is currently showing 0 for both.

**Candidate PS:** My program is working fine for only non-negative numbers

**SDEGuru:** Can you do to fix it?

**Candidate PS:** .. some lengthly rant about 2’s complement … and a complicated algorithm to make this work …

**Candidate PS:** One hack is to go on till 64. Fixed. It now shows the correct answers

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include #include using namespace std; // Given a “long” number, can you count the number of bits that are set to 1? // To execute C++, please define "int main()" int findNumBitSet(long input) { int setBits = 0; long N = input; //hard-coding 64 -- bad design for(int i = 0; i < 64; i++) { // cout << (input & 1) << endl; setBits += (input & 1); input = input>>1; } cout << "The number of setBits in " << N << " " << setBits << endl; return -1; } int main() { long input1 = 16; // answer = 1 long input2 = -1; // answer = 4 long input3 = LONG_MIN; //answer = 1 long input4 = LONG_MAX; //answer = 63 findNumBitSet(input1); findNumBitSet(input2); findNumBitSet(input3); findNumBitSet(input4); } |

**SDEGuru:** Will your code work as is for a 8 bit number

**Candidate PS:** It should work for every legal long input. so, 8-bit wont.

**SDEGuru:** Why not?

**Candidate PS:** Because an 8-bit number’s MSB can be sign extended

**Candidate PS:** So, I should have got 8.

**SDEGuru:** What is the timecomplexity of this algorithm?

**Candidate PS:** Hmm, I’ve been thinking about this. It is O(log(N)). Reason is that for any number N, we need log(N) bits to represent and there is no extra space used so “constant space” and “O(logN) time”

**SDEGuru:** Is your time complexity dependent on the value of N? Arnt you always looking at all bits, regardless of the size of N?

**SDEGuru:** Wont it be O(d) where D = number of bits in the word?

**Candidate PS:** I stand corrected. My definition was wrong

**SDEGuru:** Post interview chat! On a scale of 1 to 10, where 1 = horrible and 10 = rocked it, how did you think you did in this interview?

**Candidate PS:** 5.5/10. And Speaking from my experience, I think this is a No-Hire. I made many blatant mistakes and novice errors — errors made by beginners

**SDEGuru:** Good self assessment