Adobe Interview Question for Computer Scientists


Country: India
Interview Type: In-Person




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

Limitation: Cannot delete the last node.

if(node->next == null) return;

node->data = node->next->data;
node->next = node->next->next;
delete node->next;

- Ric February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

consider this example:
1->2->3->4
and x is 2

you code does this:
1->3->3->4 (node->data = node->next->data;)
1->3->4 (node->next = node->next->next;)
1->3(delete node.next)// this particular line is not required according to me.

- Shreyas February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Shreyas. I should have saved the node->next to a temp and delete it at the end. Deleting node->next is definitly a bug.

- Ric February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can remove the limitation of not deleting the last node by adding this :-

if(node->next == NULL)
{
free(node);
node = NULL;
}
Am I correct?

- Neha February 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Neha. How will it work? Please explain. What the last to previous node will contain(the address of the next node, will it be null if yes how)? How someone will traverse the linked list after your solution?

- Ashok March 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In above answer, how will you update an arbitrary "previous" element's ->next pointer to your "node->next" value?

Wont work. List gets broken.

Must have a doubly linked list; then its trivial.

- Nix February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's not necessary to update the pointer. Keep the same pointer, move the data (the values held by the nodes) instead.

- eugene.yarovoi February 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

node.data = node.next.data
temp = node.next
delete node.next
node.next = temp

- Anon February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction in last line
node.next = temp.next
Since we are copying everything to the current node and deleting next node..

- Jason February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction in last line
node.next = temp.next
Since we are copying everything to the current node and deleting next node..

- Jason February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction in last line
node.next = temp.next
Since we are copying everything to the current node and deleting next node..

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

while(ptr->link!=NULL)
{
ptr->info=ptr->link->info;
if(ptr->link->link==NULL)
{
free(ptr->link->link);
ptr->link=NULL;
exit(0);
}
ptr=ptr->link;
}
/* In this code i am simply shifting the node info towards left and finally freeing the last node

- narayan kunal March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea is very well known and It can be done in Constant time ,Having One limitation(If last node is passed)..Please have the following code snippet..

void delete(struct node *x) 
{
     struct node *y=NULL;
     
     y=x->next;
     x->data=y->data;
     x->next=y->next;
     free(y);
}

If you find any mistake then please let us know.

- User March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

delete(Node n)
{
if(n.next == null)
{
n=null;
return;
}
n.data = n.next.data;
delete(n.next);
}

- fearless Coder March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm surprised that none of you have mentioned how you'll move forward as well as backward of any arbitrarily given node!!
To me the catch is to assume that the list be either a doubly linked list or a circular list so that given any arbitrary node, you can traverse through all nodes.
Once any one of these assumptions is considered, rest of the solution is trivial.

- buckCherry November 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Adrs_of_node_to_be_deleted(NODE *node)
{
if(node->next == NULL)
{
printf("You are trying to delete last Node which is not possible as you should know previous node adrs to make it point to null after deleting last node..\n");
return;
}

while(node->next != NULL)
{
node->data = node->next->data;
if(node->next->next==NULL)
{
free(node->next);
node->next = NULL;
return;
}
node->next = node->next->next;
node = node->next;
}

return;
}

- Komala October 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If this linked list is a doubly linked list. Then, we can do this.

x->next->prev = x->prev;
x->prev->next = x->next;

delete x;

- Saravana Coumar M March 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If singly linked list. just copy the next node value to current node, and repeat till end, and delete the last node.

- Saravana Coumar M March 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its really simple:
what you have to do is:
1) take the next node's value in this node.
2) delete the next node.

if the given node is the last node then handle separately.
code using java.

public void delNode(Node curr)
{
	if(curr.next==null)
	{
		curr=null; // in case curr is last
		return;
	}

	curr.info=curr.next.info;
	curr.next=curr.next.next;
}

- Kartik Agarwal September 07, 2017 | 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