Amazon Interview Question for Java Developers


Country: United States




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

Find the issue with the above code and fix it.

- Jackie December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pnode ReversLinklist(Linklist s)
{
pnode p=s;
pnode q,r;
if(p==NULL) return NULL;
if(p->next==NULL ) return p;
q=p->next;
while (q!=NULL)
{ if(q->next!=NULL)
{
r=q->next;
q->next=p;
if (p==s)
{
p->next=NULL;
}
p=q;
q=r;
}
else
{
q->next=p;
return q;
}
}
}

- Anonymous December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can't find any issue from original code if the linked list does not contains any loop.

- gensay December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Same here.

- v1 December 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Problems are :
1. As mentioned by Dinesh,
2. No iterator used to iterate through linkedlist

public static LinkedList<Integer> reverse(LinkedList<Integer> current)
    { 
        LinkedList<Integer> result = new LinkedList<Integer>();
        if (current == null) {
            return null;
        }

        Iterator<Integer> li = current.iterator();
        while (li.hasNext()) {
            int next = li.next();
            result.addFirst(next);
        }
        return result;
    }

- Rajesh Taneja December 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the code is always loosing the head element in the iteration

Suppose we have following list

a -> b -> c -> d

So the reverse would be like this

d-> c-> b

- Jinal Shah December 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{code}
while(current!=null)
{
next=current.next;
current.next=prev;
prev=current; // this line is the error. this should be executed before current.next=prev
current=next;
}
{code}

- Jinal Shah December 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void reverse()
        {
            Node<E> temp=head;
            head=tail; // //swap head and tail
            tail=temp; //tail points to head
            //traverse the list swapping prev and next fields of each node
            Node<E> p=head; //create a node and point to head

            while(p!=null) //while p does not equal null
            { //swap prev and next of current node

                temp=p.next;
                p.next=p.prev; //point next pointer to previous
                p.prev=temp;     //previous point to what next used to point to
                p=p.next;//advance current node
            }

        }

- Anonymous December 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Code is wrong..

Suppose we have following list

a -> b -> c -> d

After first while loop it will be

b-> null -> c-> d

Third time loop will not execute..

- Dinesh Pant December 05, 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