Interview Question


Country: India
Interview Type: In-Person




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

You could push the values in a stack while going through the list in order and then print the values as you remove them from the stack: O(n) time and O(n) memory.

You could also keep track of the index of the last printed element and start from the beginning and traverse the list one less than that index until you print the first element: O(n^2) time and O(1) memory. I feel this is not a good solution though.

If you are allowed to alter the linked list as long as you restore it by the end of the algorithm, you can reverse the list (O(n) time O(1) memory), print it in order (O(n) time O(1) memory), and reverse it again (O(n) time O(1) memory) for a total of O(n) time and O(1) memory.

If you must use recursion, then you want something like this:

void printListInReverse(Node* head) {
  if (head != NULL) {
    printListInReverse(head->next);
    cout << head->data;
  }
}

Which is basically the first solution but using the stack frame for the function calls instead of a stack for the node values.

- djmclaugh August 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No offense to the interviewer but this question just sounds and feels dumb because the answer djmclaugh gave here is pretty much all we need here.

- Itanium August 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I mean to say there's nothing of value in this question and the answer is pretty simple.

- Itanium August 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You should reverse list, change reference in all items that next item should contain reference to previos. After that you can print all list in reverse order

- Evgeny Gorlov September 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive method without changing the list. It can even be done in iterative way.

public void printReverseLL(LinkedListNode<String> currentNode){  
		// check if the list is empty 
		if(currentNode == null){
			return;
		}
		
		// If not the end of list, then call the same function with next node
		printReverseLL(currentNode.getNext());
		
		// Print the value before returning
		System.out.print(currentNode.getValue());
	}

- Ketan Mehta September 12, 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