Interview Question


Country: United States




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

Old array = O
New array = A
// Find the pivot offset
pivot_index = O.find(A[0])

// Find the element we are actually looking for, in the old code
index = O.find(item)

// Calculate the index in the new array
new_index = (index - pivot_index) % O.len()

/*
	O = [1 2 3 4 5]
	A = [3 4 5 1 2]

	pivot_index = O.find(A[0]) // returns index of 2

	// Look for 4
	index = O.find(4)  // returns 3
	
	new_index = (3-2) % 5
	new_index = 1
*/

- rotinom April 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great answer. I think you might want this, though.

new_index = (index + O.len() - pivot_index) % O.len()

- Anonymous April 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

great!
Just O(1) to find the pivot.
After knowing the pivot, things become easy.
+1

- ninhnnsoc April 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@Anonymous:

-1 % 5 = 4

Beauty of the modulus operator

- rotinom April 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Which company? Phone? Onsite?

O(1) is impossible for finding element in sorted array. So your question is complete nonsense.

(second part might be interesting though, but not as an interview question).

- Anonymous April 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the problem description he said suppose that you have a find function that finds the value in O(1). So using that function you have to implement your own function. Makes sense to me.

- tphokas April 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agree with tphokas, but still something is missing.

Why won't find return the element in the rotated array too?

If not, how would we apply it? What would happen if we apply the given find to an unsorted array?

- Anonymous April 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Was this asked in X-files Mulder :D

- puneet.sohi April 10, 2014 | 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