Interview Question






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

p = head,
p->next and compare to head,
if ==, cicular
if null some time, terminate?

- ouyang498 May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will check for circular linked list, what if the list has a cycle like : a->b->c->d->e->c Note e points back to c.

We need to have 2 pointers move first by one and second pointer by 2 and see if they are equal any time.

- Monish May 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh.
Sorry for mistake.
That is to need O(N^2)?

- ouyang498 May 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static LinkedListNode FindBeginning(LinkedListNode head) {
		LinkedListNode n1 = head;
		LinkedListNode n2 = head; 
		
		// Find meeting point
		while (n2.next != null) { 
			n1 = n1.next; 
			n2 = n2.next.next; 
			if (n1 == n2) { 
				break; 
			}

}

- kumarasvn May 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use 2 pointers say p and q. start both from head. iterate p by 1 step ,ie, p=p.next and q by 2 steps ,ie, q=q.next.next for every iteration. if p and q are same at any iteration there exists a cycle. if they become null at some stage there is no cycle.

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

void findCircularlist(Struct node **h)
{
      struct node *p,*q;
      p=*h,q=*h;
      while(q->next!=p  && q!=NULL)
      q=q->next;
      if(q==NULL)
       //print not circular
       else
        //print circular
}

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

here the question itself contains 2 questions. from the above answers you can find if the linkedlist is circular or not. but consider the case where the linkedlist is not circular but it has a loop. this is the tricky thing. now we have to write a code for it. the logic is if in a circular path 2 cars run one at a speed of 2MpH and other at 4MpH then sometime they will meet at the starting point. so if the speed going car is started xM before the slow going car then the meeting point will be xM before the starting point. by taking this as the reference the code of the above question is:

public int detectloop(){
	node fast = head;
	node slow = head;
	while(fast != null){
		fast = fast.next.next;
		slow = slow.next;
		if(fast == slow){
			break;
		}
	}
	
	slow = head;
	while(fast != slow){
		slow = slow.next;
		fast = fast.next;
	}
	return fast.key;
}

you can verify the correctness of the code. the worst case complexity is O(n^2);

- raz December 26, 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