EMC Interview Question for Software Engineer / Developers






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

Heya,
void Delete(node *p) //p is the node to be deleted
{
 if(p->next==NULL) //last node
   p->value=-1; //mark it so that when we traverse the LL, and reach this node, we do not     consider this a part of LL
 else
 {
    p->value=p->next->value;
    p->next=p->next->next;
 }
}

- Pappu cant dance sala February 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

should the node go away (freed) or its only that the data in the node needs to go away

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

public bool remove(int key) //remove link with given key
{ //(assumes non-empty list)
Link pCurrent = pFirst; //search for link
Link pPrevious = pFirst;
while (pCurrent.iData != key)
{
if (pCurrent.pNext == null)
return false; //didn’t find it
else
{
pPrevious = pCurrent; //go to next link
pCurrent = pCurrent.pNext;
}
} //found it
if (pCurrent == pFirst) //if first link,
pFirst = pFirst.pNext; //change first
else //otherwise,
pPrevious.pNext = pCurrent.pNext; //bypass it
pCurrent = null; //delete link
return true; //successful removal
}

- Meruzhan August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

copy the content of the next node into the current node; release the memory of the next node; done

- syrus August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

terrible solution

- tej parkash August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how would you proceed?

- syrus August 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah tej, let's hear your solution.

- Anonymous August 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I thought he meant "terrific" solution.

What if the given node is last one in the list?

- Anonymous August 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I thought he meant "terrific" solution.

What if the given node is last one in the list?

- Anonymous August 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the given node is other than last one then the solution by Syrus is absolutely fine....But if u r supposed to delete the last node then I think U cant delete that particular node because in one way list u can travel only in one direction(no back traversal is possible from a given node)....

- KK August 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

good point~ so there is absolutely no way to delete the last node in this scenario huh?

- k August 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

we can check...if the given node is last node.
ex: if(curr->next==NULL){free(curr);curr=NULL;}
as we know we r freeing the memory being allocated to that pointer and making the pointer to NULL.

- jagan March 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

What I don't understand is how you even start to search when all you are getting is some key and no starting or any node address. He says head node is not given, then how do your even traverse the entire list.

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

Exactly !! How can we even think of starting somewhere without the Head node given??? Its definitely not the solution you guys discussed above.. Im wondering what is the actual solution?

- SCM October 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A node in a linked list is an object. So what is meant in this question is that one is given the object depicting the node to be deleted. And the object will have the data (key) and the pointer to the next node (object) in the linked list. So the solution given by syrus is correct if the current node is not the last node.
One modification that I can think of to go around this problem is to attach a dummy node at the end of the linked list. This way the node to be deleted can never be the last node. We just make the node to be deleted (if it's second last) the dummy node.

- Abhishek August 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Suppose v need to delete a node "r"
p=r->next;
r->data=p->data;
r->next=p->next;
free(p);

- Ankit Gupta (ankx06@gmail.com) August 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

malloc allocates contiguous blocks of memory. so lets say our original list contains nodes: { a, b, c, d, e}. Since we dont know the head, we dont know where the list begins. now we're asked to delete node 'e' (lets say) i.e. we've been given the address of the node 'e'. now if e->next == NULL, we can be certain that 'e' is the last node to be deleted. In this case, we must fix the address pointed to by the node 'd'. But how do we reach node 'd' given node 'e'?

Since malloc allocates memory in continuous chunks, cant we reach node 'd' by doing a pointer arithmetic i.e. Address of node 'd' = Address of node 'e' - sizeof(struct node). Now we can reach node 'd' and fix its next pointer to point to NULL.

the same logic can be used for any node but the first. imagine if we have only 1 node in the list and we're asked to delete this node?

- Jester December 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Heya,
void Delete(node *p) //p is the node to be deleted
{
 if(p->next==NULL) //last node
   p->value=-1; //mark it so that when we traverse the LL, and reach this node, we do not     consider this a part of LL
 else
 {
    p->value=p->next->value;
    p->next=p->next->next;
 }
}

- Pappu cant dance sala February 03, 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 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Delete(struct node *ptr)
{
struct node *temp=ptr->next;
ptr->data=temp->data;
ptr->next=temp->next;
free(temp);
return ;
}

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

1. With a single pointer in single linked list, only middle node can be deleted, the last node cant be deleted.
All the cases can be achieved under following conditions
a. either we need two pointers in single linked list
b. or possible if it is a double linked list
c. or if malloc ensures contiguous memory allocations for the nodes
Otherwise I don't any other ways to fix the problem.
If anybody knows the solution please let me know

- macky December 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well logically we can not delete the node whose address is provided as input.
so to serve the purpose the above tricks works
:)

- Vickey May 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

yup, impossible!

- Anonymous April 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

its impossible. how can we proceed without a given head node

- cyprus January 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

we can u are given a pointer to that noe that has to be deleted

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

we can u are given a pointer to that noe that has to be deleted

- Nikhil January 19, 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