Microsoft Interview Question Software Engineer / Developers


Country: India
Interview Type: Written Test


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

isn't it error, its trying to access a[99][0] here initially?

- Vannu on July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have used gcc 4.7.1 compiler & the platform is GNU/Linux.

I didn't get any error. And output is 1 but I don't know why.

- kishore on July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. I agree. I didn't see the typo. Its a typo for sure...

- Piyush on July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think it will give you some random answer. It is checking for the element at position a[99][j] in a loop, now it depends that which element is at that memory location. If ever in the whole loop that element comes out to be smaller than k then k's value will be updated else it will print 99.

- Spock on July 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't know where you see 99th element of array being accessed. When the for loop begins, both i and j will be reset to zero.

The only tricky part in the code is the static array should have 12 elements but it only has 9 elements. So it depends on how compiler will align the memory so accomodate for the missing 3 elements.

I would say output be architecture dependent.

- Piyush on July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

He had a typo on this line:

if(a[k][j]<k)

I guess the test should've been

if(a[i][j]<k)

- Mike on July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Had it been if(a[i][j]<k)
then there is nothing in the program which will trip anyone off.

- kishore on July 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i'm getting zero...

- gowda on July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah zero is the corrct ans.
here basically we are finding the smallest element in the array..
and since the partial initialization of the array is dun so all the uninitialized values vud be zero and thus zero is the rite ans
(only if v consider the typo)
else the ans vud be segmentation fault

- codinglearner on August 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here the output is '0' or any garbage value,why because a[k][j] means a[99][j] means any garbage value which may be greater than 'k' or less than 'k' so "a[k][j]>k" may be true or false.(we may get output '0' in some cases because some values of array are not intialised ,by default they are '0').

- Siva Krishna on July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

segmentation fault

- a_s on July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

as a[99][i] is not in data space it is trying to acces mem which is not prenmitted so Segmentation fault

- KD on July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Undefined behavior. I don't think segmentation fault would occur because the 2d array is allocated dynamically on the stack, not on the heap through malloc(). Could be wrong about this though.

It depends on the garbage values stored at a[99][0], a[99][1], a[99][2], and a[99][3]. If none of these values are less than 99, then k never gets updated. Otherwise it gets set to a[i][j]. Since 4<=a[i][j]<=12 and there are only 3 rows in the matrix, the new value of k will still produce garbage values.

- Anonymous on July 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

99

- neeraj on July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

99

- neeraj on July 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is undefined. The values up to the end of the array a[3][4] , i.e upto 12 values will be 0 if it is uniniitalized .However any value beyond that can give a runtime error as it will have a garbage value . The statement a[k][j] will make the array access unpredictable and hence cause a runtime error . I tested with vc++ a[99][j] had -8567433 so the program gave a runtime error .

- Nachiketa on July 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i am getting nine...... and if i replace 9 in the array with ne othr numbr then m getting 0.....

- war on July 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

99 obviously!! Since it will execute only last line "printf" since all others are a part of for in which condition is not satisfied.

- Aparna on July 26, 2013 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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