HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Lets say: nth node has value 5 and n+1th node has 10 value.
copy n+1th node value into nth node, and delete n+1th node.

- Dev July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes this is logically correct

- pankajb64 July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you don't have any node address nth node or n+1th node address... how you delete?
the function give is

void deletenode(int n)
{
}
here you can't access the any of the linked list pointers

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

The question seems vague, we need the head of the linked list to traverse it otherwise where will you search

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

If we had head pointer, then this can be solved

void DeleteNth(int n)
{
 Node current = First;

 for (int i = 0; i < n - 1; i++)
 {
  //Move pointer to n - 1th
  current = current.Next;
 }

 Node prev = current;

 current = current.Next.Next; // Move pointer to N + 1th
 
 prev.Next = current; // N - 1th node points to N + 1th
}

- nsdxnsk July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The question clearly states that we dont have a pointer to head node.
At least, for the purpose of discussion, we can safely assume that n is the address of nth node and Dev's logic is correct

- chandershivdasani July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When the (n+1)th node is null, this won't work out.

- notbad July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if you are suppose to delete the last node?

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

below is the probable answer
when creating the head pointer get the aligned memory with e.x. 128 bytes, so last 7 bits of the address will be zeros... now add the node number to be deleted to this address and convert it into int and pass to the function... in the function extract/deduct the last 7bits from the number so we will get number of node to be deleted and base address... now convert this int base address to linked list pointer address i.e.(node*)

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

probably they are getting the aligned memory with e.x. 128 bytes, so last 7 bits of the address will be zeros... now add the node number to be deleted to this address and convert it into int and pass to the function... in the function extract/deduct the last 7bits from the number so we will get number of node to be deleted and base address... now convert this int base address to linked list pointer address i.e.(node*)

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

This doesn't make any sense. Why would it be aligned to 128 bytes?

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

you have access to only one integer variable "n", other than this you don't have anything... if you want to delete a node in a linked list minimum data required id linked list "node pointer" and the node number "n" to be deleted... so you need to get both of these data in single variable... so this is the only way we can do

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

Note in the problem statement, it is said that "not have the address of the first node of the Linked list". This doesnt mean you dont have pointer to any node in the Linked List.

Probable Solution: Let us say you are on some node 'X' in this linked list. We can iterate tho' the Linked list with 2 pointers (a and b) which are 'n' nodes away moving them 1 node at a time together. When b reaches the end of the Linked list, a is n nodes from end of the list.

If we know the length of the linked list say 'l', then we can do l - n = d (distance to be used between a, b pointers) to get the 'n' node.

Once we get to the nth node, we can reset the pointers of its neighbours to delete the node.

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

I dn't think that it's possible.

- Roy July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is what i told the interviewer but he insists they have solved it.

- jaiswal.amarnath July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If there's no head pointer, how can one access to linked list?.....

- nsdxnsk July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Suppose there are 2 or more linked lists of at least the requisite length present in memory. WIthout the head pointer, it's obviously impossible to know which of those linked lists the request pertains to. Since the request could pertain to any one of the lists and you have no information on which one, it's not possible to fulfill the request. This is kind of a solid impossibility proof.

Interviewers make mistakes.

- Anonymous July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

u can make a pointer variable and declare it global

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

I think the parameter is the nth Node.then you can delete it.

void delete(Node * nth){
    Node * next = nth->next;
    if (next != NULL){
        memcpy(nth ,next ,sizeof(Node);
        free(next);
        next = NULL;
    }else{
    //how to process this case?
    }
}

- notbad July 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n is jst an int variable,may b 2,5 which indicates the n th position.

- neel suman July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As per my understanding, function will take address of nth node as input.
If it is doubly linked list,
(n-1)->rlink=(n+1)
(n+1)->llink=(n-1)
free(n)
If its circular linked list,
1. Iterate and find (n-1)th node.
2. (n-1)->link = (n+1)
3. free(n)

- pratap.raavi July 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you have the address of the nth node , to be deleted. See through logic previous pointer means nth -1 node -> next will point to the same node. Wot all u can do is store the address of nth node->next to the nth -1 node->next , with the help of two pointer .
your nth node will be deleted. I have made the program , and it's working properly.

- kk July 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Storing the nth node->next address to the nth node will update your nth -1 node ->next itself. Hence proved.

- kk July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if the node to be deleted is last node? still this works?

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

This is ridiculous.

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

+1.

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

+2

- Anonymous1 August 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

To delete a node You must have address of any previous node or node which you want to delete shouldn't we last one. If we have any previous node info or list header info then we can traverse upto that node and delete it. In case, if we don't have address of any previous node and the Node which should be deleted is not last one then we can copy n+1 th node's data to nth node and then delete n+1th node.

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

i hope the parameter is pointer to the node to be deleted.Then it is solved.just go on copying the (n+1)th node data to nth node, till the (n+2)th node is not null.when n+2 node is null,make the next part of current nth node null and using a second pointer of node type delete the last means n+1th node.that's it.it does not work if the node to b deleted is the last node.

- arc July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

temp=n.next;
n.data=temp.data;
n.next=temp.next;

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

As per my understanding, function will take address of nth node as input.

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

we cant actually delete that node....but can show that as if deleted...........we can just copy the data of next node to present node and so on...
{while (n-->link->link!=null)
n ->data =n->link->data;
n=n->link;}
if (n->link->link==null)
n->link=null;

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

oid deleteNode(Node **nodeptr){
Node *curr = *nodeptr, *prev = NULL;

if( (*nodeptr)->next != NULL){
while( curr->next ){
// 1. set previous node
prev = curr;
// 2. copy next data to current item
curr->data = curr->next->data;
// 3. advance curr
curr = curr->next;
}
prev->next = NULL;
nodeptr = &curr;
}
*nodeptr = NULL;
}

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

void deleteNode(Node **nodeptr){
    Node *curr = *nodeptr, *prev = NULL;

    if( (*nodeptr)->next != NULL){
        while( curr->next ){
            // 1. set previous node
            prev = curr;
            // 2. copy next data to current item
            curr->data = curr->next->data;
            // 3. advance curr
            curr = curr->next;
        }
        prev->next = NULL;
        nodeptr = &curr;
    }
    *nodeptr = NULL;
}

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

I am just addind to the notbad solution

void delete(Node * nth){
Node * next = nth->next;
if (next != NULL){
memcpy(nth ,next ,sizeof(Node);
free(next);
next = NULL;
}else{
//how to process this case?
// in this case, we mark the address as NULL
*nth = NULL;
}

will it work? any comments?


}
}

- Anonymous March 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What a moronic bunch.

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

So true. +100.

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

Can't u post answer instead of adding urself to the moronic bunch?

- Angel September 17, 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