Ebay Interview Question for Software Engineer / Developers


Country: United States




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

Assuming the list has at least 5 nodes: advance a pointer (call it frontRunner) 5 times. Now create a pointer to the beginning of the linked list (call it result). Now, advance both pointers one-by-one until frontRunner== NULL. e.g.:

Node* frontRunner = list->head;
const int numFromBack = 5;
for (int i=0; i < numFromBack; i++)
{
   frontRunner = frontRunner->next;
}

Node* result = list->head;
while (frontRunner)
{
    frontRunner = frontRunner->next;
    result = result->next;
}

return result;

- eugene.yarovoi January 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The code will do the job, however, you should always check for errors (in this case, null exception)

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

C doesn't have exceptions. I do check for next nodes being non null. while (frontRunner) is the same as while (frontRunner != NULL)

- eugene.yarovoi January 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if asked to use only one pointer.. then..
we can use a circular queue of size 5, keep adding elements to it,
once end is reached, just print next element in queue.
(just an addition to question)

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

Why would that situation ever occur? If you don't have one pointer's worth of extra space, you most certainly don't have room for a circular buffer. Here that buffer could be only 5 pointers + a char in size, but still...

- eugene.yarovoi January 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent!!

- Spandy February 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Java: [based on @eugene's logic]

public Node print5thNode(Node head)
   {
      Node front = head;
      Node back = head;
      int count = 0;
      while(count<=5)
      {
         if (front.next == null)
            return back;
         front = front.next;
         ++count;
      }
      while(front.next != null)
      {
         front = front.next;
         back = back.next;
      }
      return back;
   }

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

public Integer five_th() {
		if (size < 5) {
			System.out.println("The number of array is not enough!");
			return null;
		}
		Node ptr = head;
		int step = 5;
		while (step > 0) {
			ptr = ptr.next;
			step--;
		}
		Node ptr2 = head;
		while (ptr != null) {
			ptr = ptr.next;
			ptr2 = ptr2.next;
		}
		return ptr2.value;
	}

- Kevin March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void InLinkedList(linkedListClass obj)
{
linkedListClass p1;
p1=obj;
linkedListClass p5=p1.next.next.next.next;

while (p5.next!=null)
{
if(p5.next==null)
{
while(p1.next!=null)
{
System.out.printf("the value of the node is : ",p1.data);
p1=p1.next;
}
}
else
{
p1=p1.next;
p5=p5.next;
}
}
}

- shelly June 24, 2013 | 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