Amazon Interview Question for Software Engineer / Developers






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

Copy the contents of next node to current node.. and delete next node..

- Teja February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isnt that changing the memory location of the "next" node...

- XeqtR February 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Teja is right.
If we do that, it takes only O(1) in almost all the case except when deleting last node.

void deleteNode(link* root, link* node){
		if (node -> next == NULL){
			//find the previous node - O(n)
			link *temp = root;
			while (temp != NULL && temp -> next != node){
				temp = temp -> next;
			}
			if (temp == NULL) return;//not found
			temp-> next = NULL;
			delete node;
		}
		else { 
			//copy content - //O(1)
			link *temp;
			temp = node -> next;
			node->data = temp -> data;
			node->next = temp -> next;
			delete temp;
		}
	}

- Silence February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

your else statement is right but how did u get the root, the question says you are ONLY given the node to delete. One cannot cannot delete the last node with this approach. So your solution is partially correct.

- sam February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

question also states that you are "given a singly linked-list" which means you are at least given the head to the list.

- Anonymous May 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given a pointer to a node, it is not possible to go back from that node using this pointer. So it can be assumed that either the current node or one of the following node has to be deleted.

struct list *temp = *node;
*node = temp->next;
free(temp); // this will free the location occupied by the node to the deleted.

Comments ?

- rajat February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have got to be kidding me.

- Ryan February 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Given a pointer to a node, it is not possible to go back from that node using this pointer. So it can be assumed that either the current node or one of the following node has to be deleted.

struct list *temp = *node;
*node = temp->next;
free(temp); // this will free the location occupied by the node to the deleted.

Comments ?

- rajat February 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wrong solution! go check it ur self y it is wrong

- sam February 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

geeksforgeeks.org/?p=876

- Gautham February 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We always need pointer to the previous node to delete. Solution: Swap the data between current and next node, and delete the next node. This solution doesnt work if the node to be deleted is last in the list.

- Anonymous February 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the simplest solution is to swap the given node with its successor and delete the successor. it wont be a problem if it is the last node in the list. if that is the case, then just make the given node null.

- Anonymous March 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

temp is the given node then
temp->data = temp->next->data;
temp->next = temp->next->next;
free(temp->next);

does this sound right???

- chummi March 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Chummi.
Still the problem of last node exists
and with your solution it can even crash
next node is null
you want temp->next->next .... blastsss core dump ...

- Samana Srikanth March 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ptr - the pointer to the node to be deleted.
head - the pointer to the header of the list.


if (head == null) return ; // list is empty
if (ptr == head) //ptr points to the first node-delete first node.
{
head=head->next;
}
else // if the node to be deleted is not the first node
{
while(head->next != ptr && head->next != null)
head = head->next; // Traverse to the previous node of ptr.

if (head->next == null) return ;//reached the end of the list, didn't find ptr.

head->next = ptr->next; // actual delete statement.
}
free(ptr); //release ptr's memory.
}

- Sarita April 02, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sarita dear, you are not provided the head node.

- Agent May 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what does 'given a linked list' mean?

- Anonymous May 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void deletenode(NODE *p)
{
if(p->next->next==NULL)
{
free(p->next);
p->next=NULL;
}
else
{
NODE *temp=p->next->next;
free(p->next);
p->next=temp;
}
}

//p is the node which is pointing to the node to be deleted.

- yoga June 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys, if you are given a pointer to the last node and nothing else then you can't delete it. Don't try so hard. But that is a special case and I believe you must have the root node available.

- Sumeet July 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Whats wrong about just deleting that node? The previous node shall just point to null and shall be the new tail.
Whats so hard about it?

- erm October 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ERN PFB defination of pointer for ur reference.

Q:what is a pointer?
A:A pointer references a location in memory, and obtaining the value at the location a pointer refers to is known as dereferencing

so if head is not given and u r deleting last node... its ok u free the memory of last node BUT second last node->link still hav some address (address not value).now free memory do not necessary to hold NULL.It can be any thing "Garbage Value" .

- LIO February 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void deleteNode(Node *x)
{
if(x->next==NULL)
{
free(x);
x=NULL;
}
else
{
Node *temp=x->next;
x->data=x->next->data;
x->next=x->next->next;
free(temp);
}

- Prem Kagrani February 27, 2018 | 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