This is the transcript of the mock interview with Candidate DS (fresher)

SDE Guru: Hello, Welcome to the IM interview. Thank you for taking the time to talk to me.
Candidate DS: Hi..

SDE Guru: Are you ready? Do you have a pen and paper handy, we will go through a few coding excercises?
Candidate DS: Yes

SDE Guru: Before we get started can you briefly describe your background and experience?
Candidate DS: I am pursuing my M.E in computer science. I am in my final semester. I have a Bachelors in Computer Science

SDE Guru: What is your favorite programming language?
Candidate DS: C, but actually depending on the task.. it varies

SDE Guru: Good. Can you rate yourself in C on a scale of 1 to 10, 1 = worst, 10 = best ?
Candidate DS: 6

SDE Guru: Why do you say you are a 6, and what will it take to get you from 6 to 7?
Candidate DS: i have not done too many projects on C. But I am good enough if i take sufficient time for the task. If I spend some more time on C that will take me to 7
SDE Guru: Okay, Lets get to the ice-breaker. Given a “long” number, can you count the number of bits that are set to 1?

Candidate DS: The first thought I got was to compare the bits one by one. I am thinking it there is a simpler way though.
SDE Guru: ok, why dont we discuss your algorithm?

Candidate DS: I will take each bit of the number compare it with 1 for bringing out that 1 bit by right shifting it by 1 and storing it in an int and incrementing it till the end that will be the number of bits used for long

SDE Guru: hmm ok that sounds like a good plan. Can you implement your algorithm it in code?
Candidate DS: Give me a moment.

SDE Guru: Your example, 655536 = 10100000000010110000, hence the output is 5 good.
SDE Guru: How will this work for -ve numbers?

Candidate DS: I might have to modify the code a bit to make it work for negative numbers
Candidate DS: Actually, for -655536, I am getting 57 bits set with the original code

SDE Guru: is that correct?
Candidate DS: yes, I think it is

SDE Guru: How come? What is the number of bits for -1?
Candidate DS: For -1 it should be 64, In 2 s complement representation.

SDE Guru: Excellent, Lets make the program more complicated.. For binary, you were asked to get the number of 1’s right? Can you find the number of 1’s when represented as hexa-decimal? for example, 1 => 0x1 => 1 , 16 => 0x10 => 1, 17 = 0x11 => 2, and so on
Candidate DS: Ok.. for this, I will have to change the code slightly. Do I have to count the number of 1s? or non-zeros?

SDE Guru: Count the “1”‘s

Candidate DS: ok. How abt 0xABC in hexdecimal?
SDE Guru: if your decimal input = 2748 => 0xABC => 0 “nybbles” are 1.

Candidate DS: I understand, Let me code this up.
Candidate DS: I will shift rotate by 4 bits each time, instead of 1 then if this value is “0001”. That will be a 1 in hexadecimal format

SDE Guru: ok
Candidate DS: Here is the code, The output is not right, Logic seems correct though.

 
SDE Guru: Thank you for your time.

Comments

comments