## Microsoft Interview Question for Software Engineer / Developers

• 0

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

ok here is the series I found.

it is only possible when n is odd her in your case it is 5.

(n+1)/2 , n, (n-1)/2, n-1, (n-3)/2, n-2, .............and goes on like this

so 0th iteration C
so 1th iteration E
so 2th iteration B
so 3th iteration D
so 4th iteration A

and repeats the same .......
FYI: stack revert backs to original after n iterations.

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

it can not end up like : E-D-C-A-B no matter how many procedure were repeated

Because procedure does not change the order of those 5 boxes, it just like move the header pointer in the looped link list

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

ABCDEABCDEABCDEABCDEABCDEABCDE
^ ^ ^

Stack from the position ^.
Each operation = move Stack beginning position forward 2 chars.
For k times operation, the middle element ='C'+(2*k)%5

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

stack ?? how could we remove from bottom side of the stack???

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

simple array rotation and mod operation problem: (2*3)%5 = 1 --> answer Box B

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

following formula gives the index of first element after n rotations,

total = total number of elements in array
n = number of rotations

(total - ((n % total) - 1))

for this problem, there are 3 iterations and each iteration rotates the array by 3, so

(5 - (((3 * 3) % 5) - 1)) = 2, the top element is 'B'

so, the middle element is 'D'.

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

so by your method after 2 rounds top element should be B

because as per your formula:(5-(((2*2)%5)-1)) =2 thus the top element is B but its actually E so better check it

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

but for each iteration there will be 3 rotations
so, it will be (5-(((3*2)%5-1)) = 5,so top element will be E after 2 rounds
so i think Big N's formula is right

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

Hey Big N... ya your formula working fine... will you do me a favor by explaining how you have arrived at this one.... becoz tat matters the most na.... even it may be thought provocking to someone seeing this post... :)

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

"placed on top with their vertical order maintained", so it is not a rotation.

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

you just didn't get enough Vitamin, Zinc, Iron... when you grew up?

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

Iteration 1: ABCDE becomes CDEAB
Iteration 2: CDEAB becomes EABCD
Iteration 3: EABCD becomes BCDEA

D is in the middle

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

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

I agree with Karthik.
A C E B
B D A C
C E B {D}
D A C E
E B D A

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

Thats correct !

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

element index after n round = (index_of_variable + 2 * number_of_rounds) % size

At start middle element was C
Therefore, 3 (C's index) + 2 * 3 = 9 %5 = 4

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

please mention how are you reaching to that mathematical eq.?

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

Let n be the no of iterations and initial stack be numbered as :
A 1
B 2
C 3
D 4
E 5

mid position = 3
Let "pos" be the position which is at "mid" after 'n' iterations. Then we have

``(pos + 3*n)%5 = 3``

3 in LHS is because 3 positions are shifted in each iterations and 3 in RHS is for mid.

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

Initially ABCDE
Next CDEAB Next EABCD Next BCDEA...
Hence order is E then third element from E going upwards i.e. B
then third element from B i.e. D
then third element from D i.e. A
then third element from A i.e. C
then third element from C i.e. E (all these counting based on original order)
and can be generalized so on.

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

The simplified solution is to do circular right shit 3 times multipled by number of iterations
>>>2*3 and extract the middle element.
Programmatically you would need extra Stack and a Queue

Algorithm would be like the following

S2.push(S1.pop());
S2.push(S1.pop());
Q2.Enqueue(S1.pop());
Q2.Enqueue(S1.pop());
Q2.Enqueue(S1.pop());

S1.Push(S2.pop());
S1.Push(S2.pop());
S2.push(Q2.dequeue());
S2.push(Q2.dequeue());
S2.push(Q2.dequeue());
S1.Push(S2.pop());
S1.push(S2.pop());
S1.push(S2.pop());

Repeat the above procedure n times for n iterations. Implement the stack with an array has backing store and get the middle element.

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.