Morgan Stanley Interview Question for Software Engineer / Developers






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

my favorite one:


NODE* revlist(NODE *head)
{
NODE *tmp=NULL, *tmp1=NULL;
while (head != NULL)
{
tmp = head;
head = head->next;
tmp->next = tmp1;
tmp1 = tmp;
}
return head;
}

- edward March 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yep, that's good one.
just replace "return head" with "return tmp".

- dzher June 20, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverse() {
		List<String> single = new LinkedList<String>();
		List<String> reverse = new LinkedList<String>();
		
		single.addAll(Arrays.asList("a","b","c","d"));
		
		for (String s : single) {
			System.out.println(s);			
		}
		
		for (int i=single.size()-1;i>=0;i--) {
			reverse.add(single.get(i));
		}

		for (String s : reverse) {
			System.out.println(s);			
		}
		
	}

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

wrong...very worse

- sundar October 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

worst idea i ever seen

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

node* reverse(node* head){
node* pre=null, cur=head;
while(cur!=null){
cur->next = pre;
pre = cur;
cur = cur->next;
}

return pre;
}

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

wrong. After first iter, cur = null

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

static void reverse(struct node **head){
struct node *result = null;
struct node *current = *head;
struct node *next;

while(current != null){

next = current->next;
current->next = result; // first time it is null
result = current;
current = next;
}

*head = result; //get the pointer to point to the new head

}

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

void reverse()
{
struct list *p,*r;
p=NULL;
while(head!=NULL)
{
r=head->next;
head->next=p;
p=head;
head=r;


}

head=p;

}

- Ankur January 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package com.test.again;

public class LinkedList {

public static void main(String[] args) {

Node root = buildLinkedList();
Node reverse = reverseLinkedList(root);
System.out.println(reverse);

}

private static Node buildLinkedList() {
Node n1 = new NodeImpl();
n1.setData(10);
Node n2 = new NodeImpl();
n2.setData(8);
Node n3 = new NodeImpl();
n3.setData(12);
Node n4 = new NodeImpl();
n4.setData(14);
n1.setNext(n2);
n2.setNext(n3);
n3.setNext(n4);

return n1;
}

public static Node reverseLinkedList(Node root) {
if(root == null) return null;
Node rest = root.getNext();
Node temp = null;
while ( rest != null ){
temp = rest.getNext();
rest.setNext(root);
root = rest;
rest = temp;
}

return root;
}
}

interface Node {
Node getNext();

void setNext(Node n);

int getData();

void setData(int data);
}

class NodeImpl implements Node {
private int data;
private Node next;

@Override
public Node getNext() {
return next;
}

@Override
public void setNext(Node n) {
this.next = n;

}

@Override
public int getData() {
return data;
}

@Override
public void setData(int data) {
this.data = data;

}
}

- Aman June 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node * rev(Node* head)
{
Node* p=NULL,q=NULL;
while(head->next!=NULL)
{
q=head->next;
head->next=p;
p=head;
head=q;
q=head->next;
}
return head;
}

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

Node wrapper(Node input){
return reverse(input,null);
}
Node reverse(Node n, Node r){
if(n==nulll) return r;
Node temp = n.next;
n.next = r;
return reverse(temp,n);
}

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

void reverse(node **head_ref)
{
	node *first,*rest;
	if(*head_ref==NULL)
	return null;
	first=*head;
	rest=first->next;
	if(rest==null)
	return;
	reverse(rest);
	first->next->next=first;
	first->next=null;
	head_ref=*rest;

}


}

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

Java

Node reverse(Node head) {
	Node previous;
	Node current;
	Node next;		

	if (head == null || head.next == null) {
		return head;
	}
		
	previous = null;
	current = head;
	next = head.next;

	while (current != null) {
		next = current.next;
		current.next = previous;
		previous = current;
		current = next;
	}
		
	return previous;
}

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

public SLinkedList Reverse(SLinkedList head)
      {
         if (head == null)
            return head;
 
         SLinkedList cp = head;
         SLinkedList prev = null;
 
         while (cp != null)
         {
            SLinkedList next = cp.next;
            cp.next = prev;
            prev = cp;
            cp = next;
         }
         head = prev;
         return head;
      }

- Bindu Mahapatra February 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive algorithm:

public class ListTask {
    private static class Node {
        Node nextNode;
        int value;

        private Node(Node nextNode, int value) {
            this.nextNode = nextNode;
            this.value = value;
        }

        public Node getNextNode() {
            return nextNode;
        }

        public void setNextNode(Node nextNode) {
            this.nextNode = nextNode;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }
    public static void main(String[] args) {
        Node last = new Node(null, 3);
        Node nextNode = new Node(last, 2);
        Node first = new Node(nextNode, 1);

        Node node = reverseList(first, first.getNextNode());
    }

    private static Node reverseList(Node previous, Node nextNode) {
        if (nextNode == null) {
           return previous;
        }

        previous.setNextNode(null);
        Node node = reverseList(nextNode, nextNode.getNextNode());

        nextNode.setNextNode(previous);

        return node;
    }
}

- Vitaly October 29, 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