Goldman Sachs Interview Question


Country: India
Interview Type: In-Person




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

void reverse(node *root)
{
	if(!root)
		return;

	reverse(root->link);
	
	cout << root->data << endl;
}

- algos July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

reverse( node *Head)
{
if(Head==null)
return;
reverse(Head.next);
System.out.println(Head.data);
}

- dina July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

I do not agree that this solution. It is mentioned not to use any data structure. By using recursion, you are implicitly using stack.

One definiately should tell the interviewer about the above answer but should also mention it is not correct.

The correct solution could be to "reverse the list" and then print in second parse (and re-reverse it again if required)

- sandeep138 August 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

/**
	 * reverse the linkedList
	 * 		head : the head of list
	 * @param list
	 */
	public void reverse(LinkedList<T> list){
		Node<T> p = list.head.next;
		Node<T> succ = null;
		Node<T> front = null;
		while(p != null){
			succ = p.next;
			p.next = front;
			front = p;
			p = succ;
		}
		this.head.next = front;
	}

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

Start traversing the singly linked list and put the nodes in a stack till you reach the end of the list.
Then pop from the stack.
The elements will be in the reverse order to the original linked list.

- subahjit July 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Oops.
sorry guys misunderstood the question. It says reverse the linkedlist using same linked list.

void reverse(Node currNode) {
	if(currNode != null) {
		reverse(currNode.next);
		System.out.print(currNode.value + ", ");
	}
	return;
}

reverse(head);

- subahjit July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

No it was said to use same single linked list.

- Shoky21 July 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Shoky21. If you see that is what I have done.
I am using recursion to print the elements in the same linked list.

- subahjit July 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int counter = list.size();
	for(int i=0;i<counter;i++){
		
		System.out.println(list.pollLast());
	}

- eshwar July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

list1=1->2->.......
head=NULL;
temp=NULL;
while( list1 !=NULL ){
temp=getnode(list1->data);
temp->next=head;
head=temp;
list1=list1->next;
}

- email.suman.roy July 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse(LinkedList node){
if(node==null)
return;
reverse(node.next);
System.out.println(node.value);
}

- hamish July 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

recursion man.
go till end of link and while returning print the data

- go4gold July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well if allowed we can use Collections class in java to sort the LinkedList in reverse order.

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class AV {
public static void main(String[] args) {
List<Integer> ab=new LinkedList<Integer>();
ab.add(1);
ab.add(2);
ab.add(3);
ab.add(4);

Collections.reverse(ab);
for(Integer i: ab){
System.out.print(i);
}

}

}

- Atul July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think, this is good solution.

- Sharma July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Then why not following:

void reverseLL(LinkedList ll)
	{
		for (Iterator reverseIter = ll.descendingIterator(); reverseIter.hasNext();) {
			System.out.println(reverseIter.next());
			
		}
	}

I don't think intention of interviewer is to ask the java lib function.... but to ask algo.

- Nitin Taur September 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java
void reverse(Node node)
{
if(node.hasNext())
reverse(node.next)

System.out.println(node.value);
}

- Anonymous July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void reverseLinkedList(LinkedList<Integer> list){
while(list.size()>0){
System.out.println(list.removeLast());
}
}

- ouyangwanbin July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
ListIterator<String> itr = list.listIterator(list.size());
while(itr.hasPrevious()) {
System.out.print(itr.previous()+"\t");
}

- Sharma July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse your list and print its reversed version.

- Anonymous July 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void Reverse_linkedlist(Linkedlist ll )
{
int count = 0;
for(int i =1 ; i<ll.getSize(); i++)
{
Node current = ll.getHead();
while(current.getNext().getNext() ! = null)
{
current = current.getNext();
}
count++;
Node temp = current.getNext();
Node current1 = ll.getHead();
for(int j= 1 ; j<count ; j++)
{
current1 = current1.getNext();
}
temp.setNext(current1.getNext());
current1.setNext(temp);
current.setNext(null);
}

}

- Op July 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use descendingIterator to reverse LinkList in java

- Udayakumar Rajan August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for linkedlist we have methods like removelast() and addfirst()..
we can simply removelast element and add it at the first by using these methods till the first method becomes last!!

- mystery September 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printrev(struct node *head)
{
    struct node *q=NULL,*p=head;
    while(q!=head)
    {
        p=head;
        while(p->next!=q)
            p=p->next;
        printf("%d  ",p->data);
        q=p;
    }

}

- Joey September 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package sample;

import java.util.LinkedList;
import java.util.List;

public class Linked {
	public static void main(String args[]){
		List<Integer> l = new LinkedList<Integer>();
		l.add(10);
		l.add(20);
		l.add(30);
		l.add(40);
		l.add(50);
		
		for(int i = l.size() ; i > 0 ; i--){
			System.out.println(l.get(i-1));
		}
	}

}

- guru September 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

LinkedList<String> list = new LinkedList<String>();
		list.add("a");
		list.add("b");
		list.add("c");

		while(!list.isEmpty()) {
			System.out.println(list.pollLast());
		}

- Pani Dhakshnamurthy September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simplest one?

while(list.size()>0){
System.out.println(list.getLast());
list.removeLast();
}

- Prasad October 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void reversePrint(Node node){
if(node == null){
return;
}
reversePrint(node.next);
System.out.print(node.value+" ");
}

- Ramiz Raza August 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseLinkedList {
    static class Node{
        Object value;
        Node next;
        Node(Object value){
            this.value = value;
        }
        Node next(Node next){
            this.next = next;
            return next;
        }
        @Override
        public String toString() {
            return "Node(" +value +")+>"+(next!=null?next.toString():"|");
        }
    }
    static class SingleLinkedList{
        Node head;
        void reverse(){
            Node cur=head, prev=null;
            while(cur!=null){
                Node next = cur.next;
                cur.next = prev;
                prev = cur;
                cur = next;
            }
            head=prev;
        }
    }
    public static void main(String[] args) {
        SingleLinkedList l = new SingleLinkedList();
        l.head = new Node(1);
        l.head.next(new Node(2)).next(new Node(3)).next(new Node(4)).next(new Node(5));
        System.out.println(l.head);
        l.reverse();
        System.out.println(l.head);
    }
}

Output:
Node(1)+>Node(2)+>Node(3)+>Node(4)+>Node(5)+>|
Node(5)+>Node(4)+>Node(3)+>Node(2)+>Node(1)+>|

- petko.ivanov September 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Reverse the list and display it. Below is code to reverse the linked list using linked list node. No other data structure used. Once you get the linked list, you can print it out.

public LinkedListNode<Integer> reverse(LinkedListNode<Integer> head){
		LinkedListNode<Integer> previous = null;
		LinkedListNode<Integer> current = head;
		LinkedListNode<Integer> next = null;
		while(current != null){
			next = current.getNext();
			current.setNext(previous);
			previous = current;
			current = next;
		}
		return previous;
	}

- Ketan Mehta October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def printBackward(n):
	if n==None:
		return
	head = n
	tail = n.next
	printBackward(tail)
	print(head,end=' ')

- Anonymous October 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

struct node
{
    int data;
    struct node *link;
};
typedef struct node node;

stack<int> output, input;
void reverse(node* root)
{
	if(root->link != 0)
		reverse(root->link);
	output.push(root->data);
}

int main()
{
	//take inputs into your stack input
	node* r;
	//pop elements to put into a linked list
	reverse(r)
}

- Anonymous July 13, 2013 | 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