Walmart Labs Interview Question for Interns


Country: United States
Interview Type: Phone Interview




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

find beginning of node. To find beginning of node, first find the loop detection point
after the beginning of node is found, iterate the list and compare.add at the appropriate place

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

why do you need to add first. Start from any node. If you find a node whose value is less than the start node this is the head. Also while traversing insert the node at appropiate point

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

Wow..this was a question i really enjoyed coding. I hope i am not leaving any test cases still.

int insertAndReturnMin(List *p, int n)
	{
	//Null list
	if(!p)
		{
		p=new List(n);
		p->next=p;
		return n;
		}
	//single node list	
	if( p->next==p )
		{
		p->next= new List(n);
		p->next->next= p;
		return MIN( p->val, p->next->val );
		}
		
	//general case
	List *prev= p;
	p= p->next; int flag=0;
	while(1)
		{
		//non-edge case
		if( (prev->val < p->val) && (prev->val<n) && (n < p->val) )
			{
			//insert node and exit this loop
			List *temp= new List(n);
			prev->next=temp; temp->next=p;
			break;
			}
		//edge case
		else if((prev->val >p->val) )
			{
			if( (n>prev->val && n> p->val) // n is new max elem
				|| 
				((n<prev->elem && n< p->val) && flag=1) ) //n is new min elem
				{
				List *temp= new List(n);
				prev->next=temp; temp->next=p;
				
				return (flag==1)? n: p->val;
				}
			}
		prev=p;
		p= p->next;
		}
	//if not returned from above we already have node inserted	
	while( prev->val < p->val )
		{
		prev=p;
		p=p->next;
		}
		return p->val;
}

- Yoda July 08, 2012 | 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