Amazon Interview Question for Software Engineer / Developers


Country: United States




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

Well I won't say its too easy unless you are allowed to traverse once and get the head aka minimum element....Three cases will arrive in this case...
1. value is less than givenNode.data.
2. value is greater than givenNode.data.
3. value is equal to given givenNode.data.

Implementation won't be much complex...but yes it will take time...if you consider all the cases like largest element and only 1 node in the list etc..

Java implementation...
docs.google.com/open?id=0Bw6L8Yy5eLJ3WGhpRENTcHNlekk

- Anshul December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please check this test:

public static Node testList() {
	Node myList = new Node(1);// create a List
	myList.next = new Node(2);
	myList.next.next = new Node(3);
	myList.next.next.next = new Node(10);
	myList.next.next.next.next = new Node(10);
	myList.next.next.next.next.next = new Node(1);
	myList.next.next.next.next.next.next=myList; 
	return myList;
}

- New_Coder_007 December 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its working fine....what were your function arguments?

- Anshul December 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

void insert(int n, node* random)
{
    if(random==NULL)
    {
	node* newNode = getNode(n);
	head = newNode;
	return;
    }
    if(random==random->next)
    {
        node* newNode = getNode(n);
        random->next = newNode;
        if(newNode->data<random->data)
		head = newNode;
        return;
    }
    node* current = random;
    node* nextNode = random->next;
    node* newNode = NULL;
    while()
    {
        if((n>=current->data && n<nextNode->data) ||
	   (n<current->data && n>nextNode->data))
	{
	    newNode = getNode(n);
	    newNode->next=nextNode;
	    current->next=newNode;
	    break;
	}
	current=nextNode;
	nextNode=nextNode->next;
    }
    if(n<current->data && n<nextNode->data)
        head = newNode;
    return;
}

- Rahul December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

have to change the if-condition to

if((n>=current->data && n<nextNode->data) ||
(n<current->data && n<nextNode->data && current->data>nextNode->data))

- Rahul December 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Node Insert(Node node, int value)
        {
            Node current = null;
            Node tmp = null;
            Node newNode = null;

            // Empty list
            if (node == null)
            {
                if ((newNode = new Node()) == null)
                    throw new OutOfMemoryException();

                newNode.data = value;
                return (newNode);
            }

            if (node.data == value)
                return (InsertPrior(node, value));

            // Single node list
            if (node.next == null)
            {
                // Assert : node.prev == null
                return (InsertPrior(node, value));
            }

            for (current = node.next ; current != tmp ; current = current.next)
            {
                if (((current.prev.data < value) && (value <= current.data)) ||
                    ((current.prev.data > value) && (value >= current.data)))
                {
                    return (InsertPrior(current, value));
                }

                if (tmp == null)
                {
                    tmp = current;
                }
            }
            
            return (newNode);
        }

        public static Node InsertPrior(Node node, int value)
        {
            Node newNode;

            if ((newNode = new Node()) == null)
                throw new OutOfMemoryException();

            newNode.data = value;
            newNode.next = node;
            newNode.prev = node.prev;

            node.prev = newNode;
            newNode.prev.next = newNode;

            return (newNode);
        }

- Anonymous December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If two times list visit allowed the find minimum element in first visit...
then insertion is very easy ...

- MI December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Insert(list **head , int value)
{
list * itr = NULL;
list * pre = NULL;
if(*head == NULL)
{
*head = Node(value); //make new node
(*head)->next = *head;
return;
}
else
{
itr = *head;
pre = itr;
if(itr->value >= value)
{
while((itr != *root)&&(itr->value >= value))
{
pre = itr;
itr = itr->next;
}
}

while((itr != *root)&&(itr->value < value))
{
pre = itr;
itr = itr->next;
}
pre->next = Node(value);
pre->next->next = itr;
return;
}

}

- MI December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

check this solution for input:

list contains values from 100 to 200.
given pointer which points to the node stores value 150
now you need to insert value 2

Your algo will insert 2 in between 149 and 150 ??????

- Vin December 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Simple version:
public static void Insert(Node n,int value)
{
   if(n==null) return;  //Error condition.
   Node nw = new Node(value);
   if(n.next==null){  n.next=nw; return; }
   Node prev=n,next=n.next;
   while((prev.data>value&&next.data<value)||(prev.data<value&&next.data>value))
   {   prev=next;
       next=next.next;
   }
   prev.next=nw;
   nw.next=next;
}

- Anonymous December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

well this dsnt make sense to me....i tested it...and its not working in any of the cases

- Anshul December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Node:
def __init__(self, v):
self.next = None
self.val = v
def __repr__(self):
return 'val = %d' % (self.val)

def insert(val, rNode):
if rNode == None:
rNode = Node(val)
rNode.next = rNode
return rNode
cur, next = rNode, rNode.next
while cur and next:
if cur.val < next.val:
if val >= cur.val and val <= next.val:
break
elif cur.val > next.val:
if val >= cur.val or val <= next.val:
break
elif cur.val == next.val:
if cur is next or cur.val == val:
break
cur = next
next = next.next

t = Node(val)
cur.next = t
t.next = next
return cur

- lood339 December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

///
public class LinkedListOperations
{

public void insertNodeInSortedCircularLinkedList(int data, Node refNode)
{
if(refNode == null)
System.out.println("Ref Node is null");

Node smallestNode = findTheSmallestNodeInCircularLinkedList(refNode);
Node largestNode = findTheLargestNodeInCircularLinkedList(refNode);

if(data >= largestNode.getData() || data <= smallestNode.getData())
{
Node node = new Node(data, largestNode.getNext());
largestNode.setNext(node);
return;
}

while(smallestNode.getNext().getData() < data)
smallestNode = smallestNode.getNext();

Node node = new Node(data, smallestNode.getNext());
smallestNode.setNext(node);
}


public Node findTheLargestNodeInCircularLinkedList(Node refNode)
{
Node largestNode = refNode;
if(refNode != null)
{
while(!largestNode.getNext().equals(refNode) && largestNode.getData() <= largestNode.getNext().getData())
largestNode = largestNode.getNext();

return largestNode;
}

return refNode;
}

public Node findTheSmallestNodeInCircularLinkedList(Node refNode)
{
Node smallestNode = refNode;

if(refNode != null){
while(!smallestNode.getNext().equals(refNode) && smallestNode.getData() >= smallestNode.getNext().getData())
smallestNode = smallestNode.getNext();
return smallestNode;
}

return refNode;

}

public void printCircularLinkedList(Node refNode)
{
if(refNode != null)
{
Node starter = refNode.getNext();
System.out.print(refNode.getData());

while(!starter.equals(refNode))
{
System.out.print(" ");
System.out.print(starter.getData());
starter = starter.getNext();
}
System.out.println();
}

}
\\\

- Ali December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can take use of the property -- sorted linked list. we can build a skip list to shrink insert op speed (expectation O(lg(n))).

- MiameRishio December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Skip list will use at least 4n space plus building it will also be O(n)....Here this can be done in O(n) in worst case without using any extra space....

- Anshul December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void insert(circle cir, circle random){
    while(true){
        if((random.value <= cir.value && random.next.value >= cir.value)|| (random.value >= random.next.value)){
            circle tmp = random.next;
            random.next = cir;
            cir.next = tmp;
            break;
        }else{
            random = random.next;
        }
    }
}
}

- Anonymous December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry got deleted while editing it.. will repost it again ...

- AlgoFreek December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* insert(Node* random, int val )
{
Node* newNode = new Node;
newNode->data = val;
newNode->next = newNode;
//If node is empty
if( random == NULL )
return newNode;
Node* prev = random;
Node* start = random;
//For the cases where val > given node data
if( val > random->data )
{
while( (val > random->data) &&(prev->data <= random->data) && (random != start) )
{
prev = random;
random = random->next;
}
prev->next = newNode;
newNode->next = random;

}
//For the cases where val < given node data
else if( val < random->data)
{
while( (prev->data <= random->data) && ( random != start)
{
prev = random;
random = random->next;
}
while( (val > random->data) && (random != start))
{
prev = random;
random = random->next;
}
prev->next = newNode;
newNode->next = random;
}
//For the cases where val == given node data
else
{
newNode->next = random->next;
random->next = newNode;
}
return random;
}

Complexity is O(n)

- AlgoFreek December 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Walk the list starting from the given node till the values start decreasing after an increase (or) increasing after decreasing. Now, we know the starting node. Walk the list checking where to insert the given value. O(N)

- Metta December 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

chekc where the value can be inserted

func(node* head,value){
curr = head;
while(curr!=head){ 
if(value > current->data && value <=curr->next->data)
 break;
}
node* temp = new node(value);
temp->next = curr->next;
curr->next = temp;
}

- sachin May 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This comment has been deleted.

- Administrator December 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well I won't say its too easy unless you are allowed to traverse once and get the head aka minimum element....Three cases will arrive in this case...
1. value is less than givenNode.data.
2. value is greater than givenNode.data.
3. value is equal to given givenNode.data.

Implementation won't be much complex...but yes it will take time...if you consider all the cases like largest element and only 1 node in the list etc..

Java implementation...
docs.google.com/open?id=0Bw6L8Yy5eLJ3WGhpRENTcHNlekk

- Anshul December 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@AlgoFreek - When you are proposing a solution, please ensure that it will cover most of the cases(not asking for all). This solution not covered most of the cases.

- Vin December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Vinod
It should be fine to post the solutions whether or not it covers all the cases or not. The whole concept here is I have some idea and I am posting it. It's not necessary that my solution is always correct, I come to know about shortcomings by subsequent comments. People may not post any solutions in this forum if they others start postings something -ve about them.

- Mario December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@AlgoFreek...

Appreciate your work but it is not working in any case....:(
1. You have written ( random!=start ) in both cases as random will be always equal to start in the beginning it will never enter your loop.
2. You always need to return the same node as you were passed on the first place.

Did you test this code? :P

- Anshul December 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ansul - thanks for bringing that in. Just move that check to end that while loop instead of begining. If true then break that loop

- AlgoFreek December 05, 2012 | Flag


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