Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
2
of 4 vote

private static Node delete(Node head, int number) {
		
		while(head!=null && head.data==number)
			head = head.next;
		
		Node p = head;
		if(p==null)
			return head;
		while(p.next!=null){
			if(p.next.data == number)
				p.next = p.next.next;
			else
				p = p.next;
		}
		return head;
		
	}

- loveCoding January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

node* DeleteNodes(node *head, int number)
{
	if(head == null) return head;
	node *outputhead = head;
	while(head->data == number && head != null)
	{
		outputhead = head->next;
		delete(head);
		head = outputhead;
	}
	if(head == null) return outputhead;
	while(head->next != null)
	{
		if(head->next->data == number)
		{
			node *tempnode = head->next;
			head->next = tempnode->next;
			delete(tempnode);
		}
		else
		{
			head = head->next;
		}
	}
	return outputhead;
}

- Anonymous January 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

node* DeleteNodes(node *head, int number)
{
	if(head == null) return head;
	node *outputhead = head;
	while(head->data == number && head != null)
	{
		outputhead = head->next;
		delete(head);
		head = outputhead;
	}
	if(head == null) return outputhead;
	while(head->next != null)
	{
		if(head->next->data == number)
		{
			node *tempnode = head->next;
			head->next = tempnode->next;
			delete(tempnode);
		}
		else
		{
			head = head->next;
		}
	}
	return outputhead;
}

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

It is giving run time error. If all the nodes contain same value and need to delete the same.

- Kattupuchi October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

void delete(struct node **head, int value)
{
        struct node  *temp,*current = *head;

        if((*head)->data == value)
        {
                temp = *head;
                *head = (*head)->next;
				free(temp);
        }

        while(current->next != NULL)
        {
                if (current->next->data == value)
                {
                        temp = current->next;
                        current->next= current->next->next;
						free(temp);
						
                }
                else
                {
                        current = current->next;
                }
    }
}

- sakthivel Sethu,Software Engineer ,Bangalore October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You solved on the toughest question on CareerCup. Good Sethu!

- l337coder October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void delete(node *head,int n)
{
node *current=head->next;
node *prev=head;
node *temp;
while(prev)
{
if(head->value==n)
{
temp=head;
head=head->next;
prev=current;
current=current->next;
}
elseif(current->value==n)
{
temp=current;
prev->next=current->next
current=current->next;
}
else
{prev=current; current=current->next;}
free(temp)
}

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

void del(node *&p,int num)  //pass the root as p in main fn. and num as the  
{                                           //node key value to be deleted...
if(!p)
return;
else
del(p->link,num);

if(p->key==num)
{
node *q;
q=p;
p=p->link;
delete q;
}
}

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

This recursive solution does not adjust the next pointer of previous node to the node after node which is to be deleted. You only change the the p. Deleting q will not update pointer of previous node to the correct next node.

- cooldaa January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This recursive solution does not adjust the next pointer of previous node to the node after node which is to be deleted. You only change the the p. Deleting q will not update pointer of previous node to the correct next node.

- cooldaa January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple linklist irritation... and deletion logic.

- Sanjay Kumar January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{void delete(node *head,int n) { node *current=head while(current->next!=NULL) { if(head->value==n) { temp=head; head=head->next; current=current->next; } else if(current->next->value==n) { temp=current->next; current->next=current->next->next; current=current->next; } free(temp) } - nav January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wont work if the value is in the head node.

- Anonymous April 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wont work if the value is in the head node.

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

void delete(node *head,int n)
{
node *current=head
while(current->next!=NULL)
{
if(head->value==n)
{
temp=head;
head=head->next;
current=current->next;
}
else if(current->next->value==n)
{
temp=current->next;
current->next=current->next->next;
current=current->next;
}
free(temp)
}

}

- nav January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume the given number is K.Traverse through the linked list,for every number in the list xor it with K.If number xor K is 0 delete the node ,then proceed with the next node.This approach takes O(n) time and space.

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

Please ignore my post above.

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

deleteNodes(int *head, int value){
    current=prev=head;
    while(current!=NULL){
        if (current->val==value){
            prev->next=current->next;
            current=current->next;
            free(current);
        }
        else{
            prev=current;
            current=current->next;
        }
    }
}

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

public Node Delete(Node list, int data)
        {
            if (list == null)
                return null;
            Node llist = list,pre=null;
            while (llist != null)
            {
                if (llist.Data == data)
                {
                    if (pre == null)
                    {
                        list = list.Next;
                        llist = list;
                        continue;
                    }
                    else
                    {
                        pre.Next = llist.Next;
                        llist = pre;
                    }
                }
                pre = llist;
                llist = llist.Next;
            }

            return list;

}

- BVarghese February 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* If the all the nodes contain same value and also need to delete that node contains that value. For example 10 nodes contains same value like 1, we have to delete the all the node,Finally it has to show the list is Empty */
void node_delete(struct node **head, int value)
{
        struct node  *temp,*current;
while(1)
{
        if((*head)->data == value)
        {
                temp = *head;
                *head = (*head)->next;
				free(temp);
				if( (*head) == NULL)
					return ;
        }
		else
			break;
}
		current = *head;
        while(current->next != NULL)
        {
                if (current->next->data == value)
                {
                        temp = current->next;
                        current->next= current->next->next;
						free(temp);
						
                }
                else
                {
                        current = current->next;
                }
    }
}

- sakthivel Sethu,Software Engineer ,Bangalore October 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

/* Rewritten Format */
void node_delete(struct node **head, int value)
{
        struct node  *temp,*current;

		while((*head)->data == value)
		{        
			temp = *head;
			*head = (*head)->next;
			free(temp);	
			if((*head) == NULL)
				return;
		}
		
		current = *head;
        while(current->next != NULL)
        {
                if (current->next->data == value)
                {
                        temp = current->next;
                        current->next= current->next->next;
						free(temp);
						
                }
                else
                {
                        current = current->next;
                }
    }
}

- sakthivel Sethu,Software Engineer ,Bangalore October 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void delete(struct node **head, int value)
{
        struct node  *temp,*current = *head;

        if((*head)->data == value)
        {
                temp = *head;
                *head = (*head)->next;
				free(temp);
        }

        while(current->next != NULL)
        {
                if (current->next->data == value)
                {
                        temp = current->next;
                        current->next= current->next->next;
						free(temp);
						
                }
                else
                {
                        current = current->next;
                }
    }
}

- sakthivel Sethu,Software Engineer ,Bangalore October 10, 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