Interview Question for Developer Program Engineers


Country: United States
Interview Type: Phone Interview




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

This is my code: but it is not working as expected: As in the loop is being detected but I am not able to remove the loop

function Node(element){
        this.element = element;
        this.next = null;
    }
    function LList() {
        this.head = new Node("head");
        this.head.next = this.head;
        this.find = find;
        this.insert = insert;
        this.display = display;
       

    }

    function find(item){
        var curr = this.head;
        while(curr.element != item){
            curr = curr.next;
        }
        return curr;
    }

    function insert(newElem, after){
        var newNode = new Node(newElem);
        var curr = this.find(after);
        newNode.next = curr.next;
        curr.next = newNode;
    }

    function display() {
        var currNode = this.head;
        while ((currNode.next !== null) &&
        (currNode.next.element !== "head")) {
            console.log(currNode.next.element);
            currNode = currNode.next;
        }
    }

    function findPrevious(item){
     var curr = this.head;
        while(curr.next !== null && curr.next.element !== item){
            curr =curr.next;
        }
        return curr;
    }

    var furniture = new LList();
    furniture.insert("chair","head");
    furniture.insert("table", "chair");
    furniture.insert("couch", "table");
    furniture.insert("stool","couch");
    //furniture.display();

    function detectALoop(list){
        var slow = list.head;
        var fast = list.head;
        while(slow && fast && fast.next){
            slow = slow.next;
            fast = fast.next.next;

            if(slow === fast){
               removeLoop (slow, list);
                return 1;
            }
        }
        return 0;
    }

    function removeLoop(loopNode, list)
    {
        var ptr1 = loopNode;
        var ptr2 = loopNode;
        var looplen = 1,i;


        // count the number of nodes in loop
        while(ptr1.next != ptr2)
        {
            ptr1 = ptr1.next;
            looplen++;
        }
        console.log(looplen)
        ptr1 = list.head;
        ptr2 = list.head;
        for(i=0; i <= looplen; i++)
        {
            ptr2 = ptr2.next;
        }



        while(ptr2.next != ptr1.next)
        {
            ptr1 = ptr1.next;
            ptr2 = ptr2.next;
        }

        ptr2.next = null; // breaking the loop
    }


    console.log(detectALoop(furniture))
    furniture.display();

- jsduder December 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea is to have two references to the list and move them at different speeds. Move one forward by 1 node and the other by 2 nodes.

package com.dectectloops;

public class Node {
	
	Node next;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	boolean hasLoop(Node first) {

	    if(first == null) // list does not exist..so no loop either.
	        return false;

	    Node slow, fast; // create two references.

	    slow = fast = first; // make both refer to the start of the list.

	    while(true) {

	        slow = slow.next;          // 1 hop.

	        if(fast.next != null)
	            fast = fast.next.next; // 2 hops.
	        else
	            return false;          // next node null => no loop.

	        if(slow == null || fast == null) // if either hits null..no loop.
	            return false;

	        if(slow == fast) // if the two ever meet...we must have a loop.
	            return true;
	    }
	}
}

By assigning removing any element in the loop you can remove a loop, if that's what you meant.

- Akhil December 03, 2015 | 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