Expedia Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




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

I linked list is sorted then, just iterate through the list and check if you get any node with lower value then the previous node, if yes then linked list has a loop.


if linked list is not sorted then you can you two pointer fast and slow to check the loop

- sjain May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this will find whether there is loop or not in sorted linked list

boolean isLooped(node *head) {
  if (head == nul) {
    return false;
  }
  if (head->next == null) {
    return false;
  }
  node *temp = head;
  while (temp->next!=null) {
    if (temp->value < temp->next->value) {
	  return true;
	}
	temp = temp->next;
  }
  return false;
}

- sjain May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice. This works only if the input has no duplicates. For example, if the input is

1->3->3->3->(back to the 2nd node)

- x June 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks X for finding that out...
in that case we have a pointer to the first node from where element are going to duplicate. if temp == firstDupNode, then list is having loop.

if let say more than one elements are duplicate then we have to maintain the firstDupNode to the node which has the duplicate value as temp node.

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

put two pointers pointing to head. first pointer move 2 each time, the second pointer just 1. if they meet. then there is a loop.

pointer1 = head.next.next.
pointer2 = head.next.
while(pointer1 != null){
pointer1 = pointer1.next
pointer2 = pointer2.next
if pointer1 == pointer2 return has loop
pointer1 = pointer1.next
if pointer1 == pointer2 return has loop
}

- Sean.B.Wan May 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void FixFaultyNodesInSortedLinkedList()
        {
            //Prepare the mock list
            var head = new SingleLinkedList<int>() { Value = 1 };
            var cur = head;
            for (int i = 0; i < 5; i++)
            {
                cur.Next = new SingleLinkedList<int> { Value = i + 2 };
                cur = cur.Next;
            }

            cur.Next = head;
            //***************************************************

            if (head == null || head.Next == null)
                return; //return head;

            cur = head;
            while (cur.Next != null)
            {
                if (cur.Value > cur.Next.Value)
                    cur.Next = null; //fix by setting the next value null. ignore the rest of list for now.
                else
                    cur = cur.Next;
            }

            Console.WriteLine("Print head to tail.");
        }

Assuming the type of data stored in linked list is int value, above solution fits the bill.

- Nagesh Agastya April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void findLoop(Node head) {

                Node temp = head;
                while (null != temp.next) {
                        if (temp.data < temp.next.data) {
                                System.out.println("Loop is available " + temp.data);
                                return;
                        }
                }

                System.out.println("Loop is not available ");
        }

- Kapil July 12, 2017 | 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