Apple Interview Question for Software Engineer / Developers






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

Peace's Algorithm is a landmark in the history of Computer engineering. I wonder though that why restrict ourselves to jumping two nodes ? jump four nodes at a time and you get 0.25n , 8 nodes 0.125n , 16 nodes 0.0625 and so on. jump n nodes and you have got a O(1) and constant time traversal. No linear, logarithmic time no more.This moment will go down in history.

- Anonymous May 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

you are wrong man there s no solution with less than O(n)

- Anonymous September 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think you guys should get checked for dementia, you've lost your sense of sarcasm (ref www[.]newser[.]com/ story/45179/dementia-patients-often-cant-detect-sarcasm.html). OP was joking :/

- Anonymous March 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Not possible. Traverse n node takes at least O(n).

- hunt September 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In fact you can traverse in O(√n) by preprocessing the list and doing O(√n) extra work during insert (indeed an intelligent work upon which the search will be O(√n)) . The answer is "Skip List". Even it is possible to do in O(lgn) by selecting a proper number of levels . Just search in google.

- zahidbuet106 June 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The most interesting thing is that it is possible to find an element in unsorted list with O( sqrt(n) ) complexity :) a quiz for cs theory

- Sergio April 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just post your answer sqrt(n)-2 times more. Perhaps then we can find any sense in it.

- Anonymous April 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

We can use parallel processing to solve this problem. Create sqrt(n) threads and use each thread to traverse sqrt(n) numbers simultaneously!

- Anonymous January 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Seems apt provided ths is the only solution

- Anonymous March 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

In fact you can traverse in O(√n) by preprocessing the list and doing O(√n) extra work during insert (indeed an intelligent work upon which the search will be O(√n)) . The answer is "Skip List". Even it is possible to do in O(lgn) by selecting a proper number of levels . Just search in google

- zahidbuet106 June 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question incomplete. If we need to traverse all the n elements of the linked list then it can't be done < O(n).

If what they look for is O(n^0.5) then what needs to be traversed is a subset. Typically when given a linked list you don't know the total number of elements, so unless we know n or go thru the LL once to calculate it we wouldn't be able to skip around either.

- Swamy September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Murthy & Swamy:
Both of you are IDIOTs
@Murthy: Question itself is contradictory. Traversing and O(n^.5) not possible.
@Swamy: Why do we need to calculate the size of a LL?

- somaliPirate September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lol..

- Erik September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

Traverse only n^0.5 elements :P

- Erik September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

"Sir Ravindra jadeja!"

- Avenger April 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using second LL spreads uniformly with length sqrt(n)

- Anonymous September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm second

- Anonymous February 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this is a bogus question. But I did get the job :)
@samliPirate: I think apple likes idiots. btw, did you get any gold today? or did you walk the plank :D

- Murhty September 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I hope the person who interviewed and hired you were not reading this thread :) LOL

- t4 September 15, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Murthy: Congrats dude.

- Erik September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Murthy congratulations. what were the other questions ? it would be nice if you can put the questions, at least the one you can recollect. :-)
@ somaliPirate: i'm scared of pirates

- googler September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lol..

- Erik September 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I might be misleading due to me partial understanding of what a traversal could mean.

If n here could be considered as the number of iterations in the loop(for here we might have a loop something like while(node->next){...}) then I believe the following could be a solution.

//have two pointers for the linked list
//one to jump odd numbered nodes and other to jump in the even numbered nodes
traverse(node *n){
node *n_odd = n;
node *n_even = n->next;
while((n_odd) && (n_even))
printf("\n%d\n%d",n_odd->data, n_even->data);
if(!n_odd){
if(!(n_even))
printf("\n%d",n_even->data);
}
else
printf("\n%d",n_odd->data);
}

What I intended o do was to have two pointers jump alternate nodes and hence traversal would typically be n/2 which is n^0.5

This seems to be very simple question which by itself makes me worried if I have got something wrong.

- peace October 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n/2 is sqrt(n) ? how come?

- Anonymous February 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

uhhh .... n/2 == n^0.5? i don't think so.

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

Peace your a genius. Great work. I think the people who invented linked lists would be amazed at the performance of the list. I think Apple should replace Steve Jobs with you.

- war October 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A traversal is a traversal is a traversal...can't go below O(n) to traverse a linked list of size n.

- Andy October 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@peace .. n/2 is not n^0.5 ... n/2 = 0.5*n which is still O(n)..

- sang October 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@war what is the specific point(in my comment) u are addressing at while mocking at me ? Let me know. I will correct it.

@sang I am really embarrassed :-| . n/2 <> n^0.5 ---> my bad. thanks for pointing out.

- peace November 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

@peace: though the question talks about n^0.5, you really came up with nice solution which is better when compared to linear traversal. at least (n/2). Thanks :)

- master December 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what did u answer murthy??

- akon January 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The most interesting thing is that it is possible to find an element in unsorted list with O( sqrt(n) ) complexity :) a quiz for cs theory

- Sergio April 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well if you are allowed to design your own linked list then search/insert/delete operations can be done in O(log n).

- Mr. India January 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and O(log n) < O(sqrt n)
so O(sqrt n) can be used as an upper bond on O(log n)
so O(sqrt n) can be claimed safely on top of O(log n)

- Mr. India January 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Think different think Apple.

- Anonymous August 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think that Merge sort's algorithm of dividing the problem statement into half could be used .... and there is no such thing mentioned in the question about space.... so recursion is most likely the answer.

void traverse(mynode *head, mynode *tail) {
if(head->next == NULL){
//print the value stored in current head
}
mynode *temp1 = tail;
tail = get_mid(head, tail);
mynode *temp2 = tail->next;
tail->next = NULL;
traverse(head, tail);
traverse(temp2, temp1);
}

- abhishek June 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is that it is POSSIBLE to traverse a list faster than O(N), at least theoretically, using a quantum computer.

Check out the Wikipedia article for Grover's algorithm here : en.wikipedia.org/wiki/Grover%27s_algorithm

They state an amortized running time of O(n^0.5), and space of O(log n). It also should be noted that it is not deterministic. That means that there is high probability that the algorithm will return the correct result, but it is not guaranteed.

- PSN July 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

wat d hell is going on

- mr america January 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.


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