Interview Question


Country: United States
Interview Type: In-Person




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

(1) take 2 points i, j both point head of list.
(2) Increment j to j->next for K times.
(3)After that increment both pointer by single step .
(4)When the j pointer reach j->next ==null then your i pointer points to kth element from the last

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

/////////////////////////////////////////////////////////////////////////////
// Input: struct ListNode* list , int n
// Return: return the nth to last node or NULL if less than nth node.
/////////////////////////////////////////////////////////////////////////////
ListNode* findNthToLastLinklist(ListNode *list, int n)
{
ListNode *first = NULL;
ListNode *second = list;

// Move second pointer nth step.
while (n>0 && second)
{
second = second->next;
n--;
}

// Move both pointer now. Implicit error handle by check second.
if (second){
first = list;
while (second){
first = first->next;
second = second->next;
}
}

return first;
}

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

Take two pointer .initially both pointing to head or starting node.move one pointer for k-1 step and check in the middle of movement if this pointer goes to null.if null then return null. otherwise its turn to move both pointer simultaneously until second pointer next point to null.
here is the code
Node kthToLast(Node *head,int k)
{
if(*head==NULL || k<1)
{
return NULL;
}
Node *p1=head;
Node *p2=head;
for(int i=0;i<(k-1);i++)
{
if(p2==NULL)
{
return NULL
}
p2=p2->next;
}
while(p2->next!=NULL)
{
p1=p1->next;
p2=p2->next;
}
return p1;
}

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

Found the solution at somewhere.

Take a array of Nodes with the size k. (let say k=10)

Node[] node = new Node[10];
Node temp = head;
int count=0;
while(temp!=null){
node[count%10] = temp;
temp=temp->next;
count++
}

Now node array will have latest 10 nodes from the end. For 10th node you can fetch node[0].

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

1) Iterate the list once and find its length, N
2) Kth element from end is (N-k)th element from start.
3) Iterate the list again. (N-K)th element is the required element.
4) Smile. :)

- Tuco March 03, 2012 | 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