Amazon Interview Question for Software Engineer / Developers






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

There are 2 ways: recursion or iterative. I like iterative more (is O(1) space) but it's a little more complicated.

C#:
struct Node {
 int val;
 Node next; 
}

Node reverse(Node head) {
 if (head == null || head.next == null) return head;

 Node t = head, p = null;
 while (t != null && t.next != null) {
  Node n = t.next;
  Node nn = n.next;
  // change pointers
  t.next = p;
  n.next = t;
  p = n;
  t = nn;
 }

 if (t != null)
  t.next = p;

 return (t != null) ? t : p;

}

- S February 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1)push each node of L.L on to the stack and ...

2)Pop the stack and make a L.L till stack becomes empty..

- keshav February 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

well, there's a space complexity of O(n) in ur case.
we can reverse LL in-place by using 2 temporary pointers

Node *tmp1,*tmp2;
tmp1 = NULL, tmp2 = rootNode;
while(rootNode!=NULL){
   rootNode = rootNode->next;
   tmp2->next = tmp1;
   tmp1 = tmp2;
   tmp2 = rootNode;
} rootNode = tmp1;

- GekkoGordan February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class LinkedList {

protected Node head;

protected LinkedList()
{
head = null;
}

public void reverseLinkedList()
{
Node n = reverseList(head.next);
n.next = null;
}

private Node reverseList(Node node)
{
if(node.next!=null)
{
Node prev = node;
node = reverseList(node.next);
node.next = prev;
return prev;
}
else
{
head.next = node;
return node;
}

/*Stack<Node> nodes = new Stack<Node>();

while(node.next!=null)
{
nodes.push(node);
node = node.next;
}

nodes.push(node);
System.out.println("Pushed: " + node.data);

//head = null;
head = null;
head = new Node();
head.next = nodes.pop();
Node n = head.next;
Node n1 = null;

try{

while(nodes.peek()!= null)
{
Node na = nodes.pop();
n.next = na;
System.out.println(na.data);

}
}
catch(Exception ex)
{

}

return head;*/
}
}

//The Commented Code has solution using Stack. But it throws Stack Exception. I //tried the recursive approach instead. Stack works fine too, just need to check the
//StackEmpty Exception.

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

struct List
{
int data;
List * link;
};

List * reverse(List * L)
{
List *node=NULL:
if(L -> link == NULL)
{
return node = L;
}
node=reverse(L -> link);
L -> link ->link = L ->link;
L -> link =NULL;
return node;
}

- Buggy February 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in above code replace L -> link -> link = L ->link;
by L -> link ->link = L;

- Buugy February 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//'start' is the first node of the linked list
reverse (struct node *start) {
struct node *p = start;
struct node *q = start;
struct node *r = NULL;
while (q) {
q = p->next;
p->next = r;
r = p;
p = q;
}
start = r;
}

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

Here is java solution of the problem:

/**
*
* @author Brijpal Singh
* @date Feb 7, 2011
*/
public class Main {

public static void main(String[] args) {
final Node head = new Node(new Integer(1));
Node node = append(head, 2);
node = append(node, 3);
node = append(node, 4);
node = append(node, 5);
node = append(node, 6);
node = append(node, 7);
System.out.println(head);
System.out.println(revisedReverse(head));
}

private static Node append(Node node, int value) {
Node node2 = new Node(new Integer(value));
node.next = node2;
return node2;
}

/**
* Main class to reverse the linked list
*
* @param head
* @return
*/
private static Node revisedReverse(Node head){
if(head == null || head.next == null){
return head;
}

Node pre = head;
Node curr = pre.next;
Node next = curr.next;
head.next = null;

while(next.next !=null){
curr.next=pre;

pre = curr;
curr = next;
next = next.next;
}
curr.next=pre;
next.next=curr;

return next;
}

private static class Node{
private Object value;
private Node next;
/**
* @param value
*/
public Node(Object value) {
this.value = value;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return value.toString()+"-->"+next;
}
}
}

- Brijpal February 07, 2011 | 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