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 ?

- Frodo Baggins August 21, 2010 | Flag Reply
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).

- S August 21, 2010 | Flag Reply
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

- kaustubhonline15 August 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

forgot to add k = k%n

- kaustubhonline15 August 21, 2010 | Flag
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

- ashish.cooldude007 August 21, 2010 | Flag Reply


Add a Comment
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.

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