Amazon Morgan Stanley Interview Question for Software Engineer / Developers






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

how abt using 2 pointers, one travelling twice as fast as the other. (lets call them the 'hare' and the 'tortoise'). both start at node 0, wen tortoise moves to the 1st node, hare is at node 2, and wen tortoise is at the nth node , hare is at the 2nth node. While jumping to the 2nth node, if the hare sees the end of the list, then the tortoise is moved one step back. As per the moral, "slow and steady" will give you the result !

- vel March 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but doesn't the tortise win in the end??

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

that sounds about right.

- nunbit romance March 29, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* midList(Node *head)
{
struct node *fast, *slow;
slow=fast=head;
while(fast->next && fast->next->next)
{
fast=fast->next->next;
slow=slow->next;
}
return slow;
}

- Anonymous February 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code works perfectly fine for odd no of nodes. I think it will work for even no of nodes as well.

- Ans October 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for the function to find the mid of a linked list.
Initialize 2 pointer both pointing to the head of the list initially.
Now increment the first one by 2 places and keep the second one at rest. Keep moving the first pointer by 2 places and for each move of first pointer move the second pointer by one position. When the first pointer reaches to the end of linked list, second pointer will be pointing to the mid node.

- Anonymous April 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

node* mid(node* h)
{
	node *n, *m;
	if(!h) return 0;
	if(h->next==0) return h;
	n=h;
	m=h->next;
	while(m && m->next)
	{
		n=n->next;
		m=m->next->next;
	}
	return n;
}

- Code May 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool

- bbs59 July 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

cool

- bbs59 July 28, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for the Function to remove extra spaces in a string:

We can use the strtok function to get the tokens separated by spaces..(even if they are separated by multiple spaces). Store these tokens and once all the tokens are obtained, we can concatenate them with a single space between them. This way we are able to remove all the spaces and reconstruct the string with a single space between all the words.

- Amit Jain April 15, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the algo given by vel works absolutely fine.

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

I have two points to make :
- It didn't check for boundary cases.
- It did not exploit the information given for Doubly linked list. This information might not be required.

Are both singly/doubly linked list have the same implementation for this program?

- YetAnotherCoder September 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* mid(node* h)
{
	node *n, *m;
	if(!h) return 0;
	if(h->next==0) return h;
	n=h;
	m=h->next;
	while(m && m->next)
	{
		n=n->next;
		m=m->next->next;
	}
	return n;
}

- viren May 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

node* mid(node* h)
{
	node *n, *m;
	if(!h) return 0;
	if(h->next==0) return h;
	n=h;
	m=h->next;
	while(m && m->next)
	{
		n=n->next;
		m=m->next->next;
	}
	return n;
}

- viren May 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

All above programs would cause segmentation fault in
lists with odd number of elements.

Here is a better sol:

node* mid(node** head)
{
node *slow, *fast;
slow = fast = *head;
while(fast->next) // breaks at odd numbered list end
{
if(!fast->next->next) // if even numbered list
break;
slow = slow->next;
fast = fast->next->next;
}
return slow;
}

- S.Wagh October 29, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if anyone knows how to have middle of linked list in O(1)???????

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

mohit, there doesn't exits such solution that gives you the mid in one shot, without traversing the list once.

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

how about
int n = sizeof(linkedlist)/sizeof(structure);
node *walker = headOfLinkedList;
for(int i=0; i<n; i++){
walker = walker->next;
}

- mra February 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If u give such type of answers to the interviewer . He will quickly kich your ass and throw u out of the room ..

- lol July 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node * linklist_mid(node *head)
{
node *s, *f;
s = f = head;

while(f && f->next) {
s = s->next;
f = f->next->next;
}

return s;
}

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

Create a double out of single and traverse half of it. O(1.5n) = O(n)

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

struct list *p,*q;
p=ptr;
q=p;
int flag=0;
while(q!=NULL)
{
if(flag==2)
{
p=p->next;
flag=0;
}
else
{
q=q->next;
flag++;

}
}

printf("\nValue at middle %d\n",p->value);

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

O(n)=O(1.5n).
Fail.

- lion August 13, 2011 | 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