Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

You could do an iterative reversal of the linked list, and then print the reversed linked list in a loop. Then, reverse the list again before you return in order to restore the original state of the list.

This approach requires you to modify the list. You won't be able to do that if the list is immutable, or if other threads might be reading the list at the same time as you execute this algorithm.

Reversing a linked list and printing it out once it's reversed both take O(n) time, so the algorithm is O(n) time overall. The space used (in addition to the space occupied by the input) is O(1) as reversal can be implemented iteratively in O(1) space, and so can printing each node.

- eugene.yarovoi December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with Eugene...Here is an implementation in java...I did not reverse the list back....u might not wanna use it if other threads are using the same list as well....if not...you can call the method again to reverse the list back...algo it still O(n)

docs.google.com/open?id=0Bw6L8Yy5eLJ3T3JZNDRYbTBQbmM

- Anshul December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Considering the question as is, Evgeny's solution seems to be the right choice to eliminate the recursion, as well as the space overhead of O(n).

- ashot madatyan December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Push value of each node to a stack as you traverse the list. At the end of the list, pop values in the stack and print them.

- ash December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Push each node into the stack till end of the list in a while loop.
Pop each element from the stack and print till stack empty.

- srini December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

dsfsdfsdfsdfsd

- dfsdfsdfds December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

usage of stack is same as recursion.isn't it ? both will increase the memory complexity..Just looking for an optimized solution..

- adam December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

One might not consider it to be quite the same as recursion, so it's possible this could have been what the interviewer was looking for. But yes, both approaches require O(n) space.

For an approach requiring O(1) space and O(n) time, see my solution. Be advised, however, that my solution has certain limitations, as I note.

- eugene.yarovoi December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@adam Your point is good but I don't think it is recursion. Recursion is (mostly) implemented with stack DS but that doesn't necessarily mean every procedure implemented using stack is recursive. For example 5! = 5 * 4! is a recursive definition but if we implement a procedure that first pushes elements from 5 to 1 in a stack and then multiplying them by popping one by one wouldn't be considered as a recursive solution I guess. Due to the nature of SLL this problem fits very well to recursion and if recursion can not be used stack is a natural alternative. I think that's what made you think using stacks is equal to recursion. But as I said your point is good and it's possible for the interviewer not to like this solution.

- heuristican December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverse(curr, prev):
  while (curr):
    next=curr.next
    curr.next=prev
    prev=curr
    curr=next
  return prev

def print(head):
  while(head):
    print head.val

def reverse_print(head):
  head=reverse(head, None)
  print(head)
  reverse(head, None)

- gen-y-s December 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. reverse iteratively and then print
2. put into stack and print the elements by popping the element from the stack

- foobar December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it is just a matter of printing the elements in reverse order, we can do the following:

String s = new String();
Node n = head;
while (n != null) {
   s = new String(n.data) + ", " + s;
   n = n.next;
}

- famee December 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you are using recursion

- Ava December 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, that's recursion.

- Daniel Hernandez December 04, 2012 | Flag
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