Amazon Interview Question for Quality Assurance Engineers


Country: India
Interview Type: In-Person




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

Consider

public class LinkedListNode {

  int data;
  LinkedListNode next;
}

Reverse function should be like that:

public LinkedListNode reverse(LinkedListNode head) {
    if (head == null) {
      return head;
    }
    LinkedListNode previous = null;
    LinkedListNode current = head;
    while (current != null) {
      LinkedListNode t = current.next;
      current.next = previous;
      previous = current;
      current = t;
    }
    return previous;
  }

- Mike L February 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Iterative:
youtube.com/watch?v=uJZMxWhYTJk

Recursive:
youtube.com/watch?v=MRe3UsRadKw

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

Iterative:
youtube.com/watch?v=uJZMxWhYTJk

Recursive:
youtube.com/watch?v=MRe3UsRadKw

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

I have question. Why the linked list did not include a pointer variable? the variable next should be a pointer variable, right?

- ajaNacs February 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}

- Anonymous February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
		current.setNext(prev);
		if(next == null)
			return current;
		 return reverse(current,next,next.getNext());

}

- Anonymous February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
		current.setNext(prev);
		if(next == null)
			return current;
		 return reverse(current,next,next.getNext());	
	}

- arvind kumar dhariwal February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
		current.setNext(prev);
		if(next == null)
			return current;
		 return reverse(current,next,next.getNext());

}

- arvind kumar dhariwal February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

```private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}
```

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
		current.setNext(prev);
		if(next == null)
			return current;
		 return reverse(current,next,next.getNext());

}

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{ private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}}

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node* reverse(Node* head)
{
	Node *rev=NULL;
	while(head!=NULL)
	{
		curr = head;
		head = head->next;
		curr->next=rev;
		rev=curr;
	}
	return rev;
}

- Vishal S Kumar February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public Node* reverse(Node* head)
{
	Node *rev=NULL;
	while(head!=NULL)
	{
		curr = head;
		head = head->next;
		curr->next=rev;
		rev=curr;
	}
	return rev;
}

- Vishal S Kumar February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

not able to submit the code but below is pseudo code ::

LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next)
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {current.setNext(prev); if(next == null) return current; return reverse(current,next,next.getNext());

}

- arvind February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package LinkedList;

import java.util.LinkedList;

public class LinkedListNode {

	int value;
	LinkedListNode next = null;

	public LinkedListNode(int i) {
		this.value = i;
	}

	public LinkedListNode addNode(int i) {
		this.next = new LinkedListNode(i);
		return next;
	}

	public LinkedListNode getNext() {
		return next;
	}

	@Override
	public String toString() {
		String restElement = value+"->";
		LinkedListNode newNext = getNext();
		while(newNext != null)
			{restElement = restElement + newNext.value + "->";
			newNext = newNext.getNext();}
		restElement = restElement +newNext;
		return restElement;
	}

	public static void main(String[] args) {
		LinkedListNode headnode = new LinkedListNode(1);
		headnode.addNode(2)
		.addNode(3)//.addNode(4).addNode(5).addNode(6)
		;

		System.out.println(headnode);
		headnode = reverse(null,headnode,headnode.getNext());
		
		System.out.println(headnode);
	}

	private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
		current.setNext(prev);
		if(next == null)
			return current;
		 return reverse(current,next,next.getNext());	
	}

	private void setNext(LinkedListNode prev) {
		this.next = prev;
		
	}
}

- avi.jasu01 February 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my solution:

public class Link {

	public int number;
	public Link next;
	

	public Link(int number) {
		this.number = number;
		this.next = null;
	}

	public void display() {
		System.out.println(number);
	}

	public String toString() {
		return Integer.toString(number);
	}

	public static void main(String[] args) {
		LinkList as = new LinkList();
		as.insertFirstLink(1);
		as.insertFirstLink(2);
		as.insertFirstLink(3);
		as.insertFirstLink(4);		
		as.display();
		as.reverse(as);
		System.out.println("----");
		as.display();
	}
}

class LinkList {

	public Link firstLink;

	public LinkList() {
		this.firstLink = null;
	}

	// Add element to Link List
	public void insertFirstLink(int number) {

		Link newLink = new Link(number);
		// Reference to first link
		newLink.next = firstLink;
		// first link is now equal to last link added
		firstLink = newLink;
	}

	public void display() {

		Link theLink = firstLink;
		
		if(theLink == null){
			System.out.println("Empty LinkedList");
		}

		while (theLink != null) {
			theLink.display();
			System.out.println("Next link: " + theLink.next);
			theLink = theLink.next;
			System.out.println();
		}
	}

	public void reverse(LinkList data){
		Link curren = firstLink;
		Link prev = null;
		Link next = null;
		
		while(curren != null){
			next = curren.next;
			curren.next = prev;
			prev = curren;
			curren = next;
		}
		
		firstLink = prev;
	}
	
}

- José February 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ReverseLinkedList {

Node l;
Node f;
int size = 0;

public void add(int i) {
Node newnode = new Node(l, i, null);
size++;

if (l == null) {
f = newnode;
} else {

l.next = newnode;
}

l = newnode;
}

public int get(int i) {

Node currentNode = l;
for (int j = 0; j <= size; j++) {
if (i == j)
return currentNode.i;
currentNode = currentNode.prev;

}
return -1;
}

private class Node {
int i;
Node prev;
Node next;

Node(Node prev, int i, Node next) {
this.prev = prev;
this.next = next;
this.i = i;

}

}

}

- poola.praveen February 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ds.linklist.op;

/**
*
* @author lokhavij
*/
public class SingleLinkList {

Node start;
Node current;
int length;

public void add(int data){
Node node;

if(start==null){
start=new Node(data);
current=start;
length=1;
}
else{
current.next=new Node(data);
current=current.next;
length++;
}
}

public void delete(int data){
Node item=start;

if(item.data==data){
start=item.next;
}
else{
while(item!=null) {
if(item.next.data==data){
item.next=item.next.next;
length--;
break;
}

item=item.next;
}
}
}


public void print(){
Node item=start;
while (item!=null) {
System.out.println(item.data);
item=item.next;
}
}

public int getCount(){
return length;
}


public void reverse(){

Node prevTmp=null;
Node nextTmp=null;
Node headTmp=start;


while(headTmp!=null){
nextTmp=headTmp;
headTmp=headTmp.next;
nextTmp.next=prevTmp;
prevTmp=nextTmp;
start=nextTmp;
}

}

public static void main(String[] args) {

SingleLinkList linkList=new SingleLinkList();

linkList.add(0);
linkList.add(1);
linkList.add(2);
linkList.add(3);
linkList.add(4);
linkList.add(5);
linkList.print();

System.out.println("length ->"+linkList.getCount());

linkList.reverse();
linkList.print();

}

}

- Vijay Lokhande February 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ds.linklist.op;

/**
*
* @author lokhavij
*/
public class SingleLinkList {

Node start;
Node current;
int length;

public void add(int data){
Node node;

if(start==null){
start=new Node(data);
current=start;
length=1;
}
else{
current.next=new Node(data);
current=current.next;
length++;
}
}

public void delete(int data){
Node item=start;

if(item.data==data){
start=item.next;
}
else{
while(item!=null) {
if(item.next.data==data){
item.next=item.next.next;
length--;
break;
}

item=item.next;
}
}
}


public void print(){
Node item=start;
while (item!=null) {
System.out.println(item.data);
item=item.next;
}
}

public int getCount(){
return length;
}


public void reverse(){

Node prevTmp=null;
Node nextTmp=null;
Node headTmp=start;


while(headTmp!=null){
nextTmp=headTmp;
headTmp=headTmp.next;
nextTmp.next=prevTmp;
prevTmp=nextTmp;
start=nextTmp;
}

}

public static void main(String[] args) {

SingleLinkList linkList=new SingleLinkList();

linkList.add(0);
linkList.add(1);
linkList.add(2);
linkList.add(3);
linkList.add(4);
linkList.add(5);
linkList.print();

System.out.println("length ->"+linkList.getCount());

linkList.reverse();
linkList.print();

}

}

- vijay.lokhande.in February 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using the power of recursion and dynamic programming

public void ReverseALikedList(myLinkedList<int> number)
        {
            if (number == null) return;
            ReverseALikedList(number.Next);
            CreateReverseLinkedList(number.data);
        }

        public LinkedList reversedLinkedList = new LinkedList();
       

        private void CreateReverseLinkedList(int data)
        {
            reversedLinkedList.AddLast(data);

}

- maksymas March 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Public static Node Reverse(Node head)
{
	Node current = head;
	Node prevNode = null;
	Node nextNode = null;

	While ( current != null)
	{
		nextNode = current.next;
		current.next = prevNode;
		prevNode = current;
		current = nextNode;	
	}
	return prevNode;

- Anonymous May 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverseList(head):
prev = None
curr = head
while curr.next:
temp = curr.next
curr.next = prev
prev = curr
curr.next = temp
head = prev
return prev

- Swati Goyal May 25, 2018 | 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