## unknown Interview Question for Software Engineer / Developers

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

Is there any in space O(n) algo for this problem ?

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

if k = 0 return array;
if k > n, k = k % n.
save the first k if 2k < n or save the last 2k-n is 2k > n, play with the rest of the array and then copy the saved array;
if k < 0, rotate in the opposite direction based on the same logic.
SC: O(n); TC: O(n).

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

yes!
1) reverse the array.
2) reverse 0 to (k-1)th indexed part of array.
3) reverse k to (n-1)th indexed part of array.

original array 2 7 9 1 8 7 3
if we have to rotate right by 2 units. (k=2).

step 1: reverse it
3 7 8 1 9 7 2

step 2: reverse 0 to 1 st index
7 3 8 1 9 7 2
step 3: reverse 2nd to last
7 3 2 7 9 1 8

this is the required solution

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

forgot to add k = k%n

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

suppose array is a1,a2,,an
we want to rotate with k index..a1 to ak is A and ak+1 to an is B;
reverse(A)(B);
(A)reverse(B);
reverse(AB);
example
2 3 4 5 6 7 8 9
k=4;
5 4 3 2 6 7 8 9
5 4 3 2 9 8 7 6
6 7 8 9 2 3 4 2

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.