Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

following checks should be done before proceeding

null check on list
N <= 0, throw custom exception
N == 1, we need to delete all the nodes
Circular check using Floyd's cycle detection method or any other method

if not circular, its easy

int count = 0;
            
            while (current != null)
            {
                count++;

                if (count % n == 0)
                {
                    current.prev.next = current.next;

                    if (current.next != null)
                        current.next.prev = current.prev;
                }

                current = current.next;

}

or we need to find the start of the loop and check for it when we iterate through the list and delete

- siva October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

may be you would like to include
free(current);

- sp!de? October 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution will not work.You have to increment the counter twice every time you delete a node..Please do check my solution.

- suvrokroy January 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The question is quite unclear to me,

1. is it circular dll. ?
2. Delete every Nth node means delete the nodes untill only one node is left.
3. If you delete Nth node then your next count should start from (N+1) th node?

- Psycho October 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is this circular doubly linked list?

- JustCoding October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Delete the Nth node and the next (N-1)th node , next (N-2)th node and so on unless you come to end.

- loveCoding October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct doubly_link_node
{
	struct doubly_link_node *next;
	struct doubly_link_node *prev;
	int data;
};

void remove_nth_doubly_link_node(struct doubly_link_node **head, unsigned int n)
{
	struct doubly_link_node *curr = *head;

	while (n-- && curr != NULL) {
		curr = curr->next;
	}

	if (curr != NULL) {
		if (curr->prev != NULL) {
			curr->prev->next = curr->next;
		} else {
			*head = curr->next;
		}

		if (curr->next != NULL) {
			curr->next->prev = curr->prev;
		}

		free(curr);
	}
}

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

Q1: Every nth node from start or from end or both???

Q2[just a corner case :)]: Do we need to delete all nth node from initial condition of given list
OR
Do we need to delete all nth node at any stage
for ex if if have
1..2..3..4..5..6..
I have to delete each 3rd node
so I deleted 3rd node and list became
1..2..4..5..6..
now again deleted 3rd node
1..2..5..6..
and so on
1..2..

- PKT October 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void getNthNode(Node node,int count,int limit)
{
      if(node!=null)
      {
}
}

- Eswar October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void getNthNode(Node temp,int count,int level)
{
     if(temp!=null)
	 {
	    if(count==level)
		{
		      Print(temp.data);
			   getNthNode(temp.next,1,level-1);
		}
		else
		{
		    getNthNode(temp.next,count+1,level);
		}
	 }

}

getNthNode(head,1,N);

Please some one let me know if the above code works or not.

- Eswar October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry for posting it multiple times. I updated the code to delete the every Nth node.

void getNthNode(Node temp,int count,int level)
{
     if(temp!=null)
	 {
	    if(count==level)
		{
		      temp.prev.next=temp.next;
			  temp.next.prev=temp.prev;
			  getNthNode(temp.next,1,level-1);
		}
		else
		{
		    getNthNode(temp.next,count+1,level);
		}
	 }

}

getNthNode(head,1,N);

Please some one let me know if the above code works or not.

- Eswar October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void deleteNthNode(Node head, int N) {
if ( head == null )
return;
if ( N == 1 ) {
head = head.next;
head.prev = null;
}

Node node = head;
for ( int i = 0; i < N && node != null; i++ )
node = node.next

if ( node == null )
return;
if ( node.next == null ) {
node.prev.next = null;
return;
}

node.prev.next = node.next;
node.next.prev = node.prev;
}

- Hua October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just a bit change to your code to delete every nth node :

public void deleteNthNode(DLinkList head, int N) {
if (head == null)
return;
if (N == 1) {
head = head.next;
head.prev = null;
}

DLinkList node = head;
for (int i = 0; i < N && node != null; i++)
node = node.next;

if (node == null)
return;
if (node.next == null) {
node.prev.next = null;
return;
}

node.prev.next = node.next;
node.next.prev = node.prev;
deleteNthNode(head, N);
}

- Anonymous October 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

currN = head;
currN2=taill;
pos=0;
ilen=len(dlist); npos=ilen;
while(currN != null)||(pos<=ilen/2)
	if(pos%reqPos) == 0
		(currN->prev)->next) = currN->next;
		(currN->next)->prev) = currN->prev;
	if (npos%reqPos) == 0
		(currN2->prev)->next) = currN2->next;
		(currN2->next)->prev) = currN2->prev;
	currN = currN->Next;
	currN2 = currN2->Next;
	pos++;
	npos--;

- Rama Chandu October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void deleteNthNode(LNODE *head, int n)
{
LNODE *p = NULL;
LNODE *q = NULL;
int i;

if(head == NULL)
return;
if(n == 0)
return;

p = head->next;
for(i=n-1; i>0 && p!=NULL; i--)
{
p = p->next;
}


if(p == NULL)
return;
if(p != NULL)
{
p->pre->next = NULL;
p->pre = NULL;

q = p->next;
while(q != NULL)
{
free(p);
p = q;
q = p->next;
}
}
}

- bin October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DoubleLinkedList {

	private class Node{
		
		int data;
		Node prev;
		Node next;
		
		public Node(int d)
		{
			data=d;
			prev=null;
			next=null;
		}
		
	}
	
	Node first;
	Node last;
	
	public void insert(int d)
	{
		Node newNode= new Node(d);
		
		if(first==null)
		{
			first=newNode;
			last=newNode;
			return;
		}
		
		last.next=newNode;
		newNode.prev=last;
		last=newNode;
	}
	
	public void display()
	{
		Node current=first;
		while(current!=null)
		{
			System.out.println(current.data);
			current=current.next;
		}
	}
	
	public void deleteNthNode(int N)
	{
		
		Node current=first;
		
		while(current.next!=null)
		{
			for(int i=1;i<N;i++){
				
				if(current.next!=null)
					current=current.next;
				else
					break;
			}
			
			if(current.next==null)
			{
				current.prev.next=null;
				current.prev=null;
				return;
			}
			
			current.prev.next=current.next;
			current.next.prev=current.prev;
			
			current=current.next;
			
		}
	}
	public static void main(String[] args) {
		
		DoubleLinkedList dll= new DoubleLinkedList();
		
		dll.insert(1);
		dll.insert(2);
		dll.insert(3);
		dll.insert(4);
		dll.insert(5);
		dll.insert(6);
		
		dll.deleteNthNode(3);
		dll.display();
	}

}

- codingAddicted October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void deleteEveryNthNode(int i) {
		
		System.out.println("####Deleting every "+i+"th Node......");

		LinkedList temp=head;
		int count=1;
		if(i<=0)
			return;
		else if(head==null)
			return;
		else if(i==1){
			head=null;
			return;
		}else{
			
			while(temp!=null){
				LinkedList p=temp;
				if((count+1)%i==0){
					
					if(p.next==null)
						p=null;
					else if(p.next.next==null)
						p.next=null;
					
					else if(p.next.next!=null)
					p.next=p.next.next;
					
					count++;
				}
				
				temp=temp.next;
				count++;
				
			}
			
		}
		
		
	}

- suvrokroy January 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void deletenth(dnode **headref, int n)
{
    int count=1;
    while(count!=(n+1))
    {
        dnode *tmp=*headref;
        if(tmp->next==NULL)  //if you reach the last element
        {
            *headref=NULL;
        }
        else
        {
            tmp->next->prev=NULL;
            *headref=tmp->next;
        }
        delete(tmp);
        count++;
    }
}

- Anonymous May 20, 2013 | 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