NVIDIA Interview Question for Software Engineer / Developers






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

public void deleteNode( Node currentNode)
{
if(currentNode.next==null)
{

/*This means we are on the last node, if we make this node null, so the previous node that points to it
* we get null in the next pointer. This way this node will be removed from the list
*/
currentNode=null;

}
else
{
/*all we know about a the current node is its value and the next pointer
so if we can update the value of the node then we have overridden the value of
the node we wanted to delete*/
// step 1:
current.value= current.next.value;

/*now we need to update the pointer, since we already got the value of next node in out current node, we don't need that node.
But, we sure want to remove it from the list. So, we will update the current node's next pointer to the next node's
next pointer thereby removing the next node
*/
// step 2:
current.next= current.next.next;
}

}

- Miss N April 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can't we just set free(lastnode) if the node is last? what is the problem in that? It works fine.

- Anonymous July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

what is the pointer given wasn't the pointer from the previous node , it was some random pointer(we don't have the luxury to directly set the given pointer to NULL or set it to node next to the one that gets deleted )

- k53sc May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if ....

- k53sc0 May 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

haha..+1 for comment above :D

- mahi July 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Miss N: how can we make a node null?? (in case its a last node)

- camSun April 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am sorry, if it is last node you cannot delete it as you don't have any access to previous pointers as well as the nest pointers. Also, you cannot make a node null as it did out of mistake.
Sorry about that!

- Miss N April 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Normally, in a singly linked list problem, we have access to header of the list. So for the problem in hand, if we have the pointer to the node to be deleted and the HEADER, we can delete the last node as well.

//code snippet:
node p=header;
while(p.next!= pointerToNodeToBeDeleted)
{
p=p.next;
}
p.next=null;

- sks April 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A minor update: what if the node to be deleted is not the last node?

Just change the last line of code "p.next=null" to

p.next=pointerToNodeToBeDeleted.next;
pointerToNodeToBeDeleted.next=null;

This will take care of both last and other nodes.

- sks April 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Although we have virtually deleted the node....we have not yet freed the node from the memory.
what about freeing the node?is it possible to free it or no?

- rohan1357 May 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void deletenode(struct node* nodetobeDeleted) {
struct node* temp = nodetobeDeleted->next;
nodetobeDeleted->data = temp->data;
nodetobeDeleted->next = temp->next;
free temp;
}
We cannot delete the last node without making use of head pointer.

- SS May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is how this problem is designed, and it's the answer.

- SAN May 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@SS : what is the problem in deleting the last node?

- mrn August 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if ( node->next! = NULL) { // check if the node is not the last node
while(node->next!=NULL){
node->value = node->next->value;
if(node->next->next == NULL) {// check if the node is the second last node
free(node->next); // always free the last node after adjusting node-> value
node->next = NULL;
break;
}
node= node->next
}

}else { // if the node is the last node
NODE * temp;
temp = node;
node =NULL;
free(temp);

}

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

I assume that it is a singly linked list. So what we can do is since we have the pointer to the node which we have to remove, we can copy the data of the next node into the present node and increment the pointer and remove that node. If the next node is null then just remove the present node.

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

LOL

- Anonymous October 15, 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