unknown Interview Question for Applications Developers

Country: India
Interview Type: In-Person

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

This solution is similar to the first example. If you advance a fast pointer twice as often as you advance a slow pointer, you'll find the middle element when the pointer reaches the end. The way I wrote this code assumes that when the length of the list happens to be even, we return length / 2 - 1.

``````private static Node getMiddleElement(Node head) {
Node slowCurrent = head;
Node fastCurrent = head;

while (fastCurrent != null && fastCurrent.next != null && fastCurrent.next.next != null) {
fastCurrent = fastCurrent.next.next;
slowCurrent = slowCurrent.next;
}

return slowCurrent;
}``````

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

What's the reason of condition inside while loop ? Can't it be done only with this condition
while(fastCurrent.next!=null){
}

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

initialize two pointers to the head, iterate "slow" pointer by 1 and "fast" pointer by 2. When the fast pointer reaches the end, the "slow" pointer is at mid.

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

Node* first=root;
Node* second=root;
Node* prev=null;
while(second.link!=null){
prev=first;
first=first.link;
second=second.link;
if(second.link!=null)
second=second.link;
else{
printf(prev);//printing middle two nodes for even number of nodes present
printf(first);

}

}
printf(first);// printing middle node for odd number of nodes in list

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

``````private static Node getMiddleElement(Node head) {
Node slowCurrent = head;
Node fastCurrent = head;

while (fastCurrent != null && fastCurrent.next != null && fastCurrent.next.next != null) {
fastCurrent = fastCurrent.next.next;
slowCurrent = slowCurrent.next;
}

return slowCurrent;
}``````

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

``````private static Node getMiddleElement(Node head) {
Node slowCurrent = head;
Node fastCurrent = head;

while (fastCurrent != null && fastCurrent.next != null && fastCurrent.next.next != null) {
fastCurrent = fastCurrent.next.next;
slowCurrent = slowCurrent.next;
}

return slowCurrent;
}``````

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

Really sorry about the duplicate posts. The page wasn't reloading...

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

just delete the duplicate answers, careercup allows to do that.

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

Node* findMiddleNode(Node* start)
{
if(start)
{

Node* ptr1 = start;
Node* ptr2 = start;
while( ptr2->next != NULL)
{
ptr1 = ptr1->next;
ptr2 = ptr2->next->next;
}
return ptr1;
}
else
{
return NULL;
}
}

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

void middlenode(struct node *root)
{
struct node *p,*ptr;
int count1,count2;
count1=0;
count2=0;

root=root->next;
for(ptr=root;ptr;ptr=ptr->next)
{
p=ptr;
count1++;
count2=0;
while(p->next)
{
p=p->next;
count2++;
}

if(count2==count1)
{
printf("MIDDLE NODE IS %d",ptr->info);
break;
}

if(count1>count2)
{
printf("NO MIDDLE NODE IS EXISTING");
break;
}

}

}

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

``````public V getMiddleElement(){
Node<V> fast= firstNode;
Node<V> slow = firstNode;

while(fast!=null && fast.next != null){
fast= fast.next.next;
slow = slow.next;
}
return slow.value;
}``````

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