Amazon Interview Question for Software Engineer in Tests


Country: India
Interview Type: Written Test




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

Corrected a simple issue to print the tail list in reverse order.

int counter=0;
Public void printList(Node root, int k)
{
If(root!=null)
{
printList(root.next);
counter++;
if(counter<=k)
Print root.data;
}
}

The above algorithm takes O(n) time.

- Prasad April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice Solution.

- GT1 October 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not seeing how this is printing the last k nodes in reverse. It looks like it only print the first k nodes.

1) if will give exception when root length is < k

2) it's a single linked list and it can't go backward.

3) k only says number of nodes to print, not the position in the list.

I think you need to create an array with size of k. Fill the array from the list root[root.length-k] to root[root.Length] then print the array in reverse. That's O(n*m), n is the size of the single linked list. m is the size the temporary array.

If it's a double linked list, you can start from the back and do root.prev k-th times to print data from each node, but that's too easy.

- visitor June 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Stack will make the code more readable and simple as well:

public static void PrintKnodeTillTail(LinkedList list, int k){
            if (k < 1 || list == null)
                return;
            LinkedList regularPtr = list;
            LinkedList kAheadPtr = list;
            // Move the first pointer K nodes ahead
            for (int i = 0; i < k; i++){
                kAheadPtr = kAheadPtr.Next;
                if (kAheadPtr == null)
                    return;
            }
            // when kAhead reaches end regularptr reaches K node before end
            while (kAheadPtr != null){
                kAheadPtr = kAheadPtr.Next;
                regularPtr = regularPtr.Next;
            }
            // push the K nodes to stack
            Stack<LinkedList> stk = new Stack<LinkedList>();
            while (regularPtr != null){
                stk.Push(regularPtr);
                regularPtr = regularPtr.Next;
            }
            // pop and print them
            while (stk.Count > 0)
                Console.WriteLine(stk.Pop().Data);
        }

- vj April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Improved solution, using Prasad's code without the need for an additional external counter field: we can pass k by pointer/reference and decrement it within the function. Adapted for C#:

public void GetLastLL2(LLNode node, ref int k)
{
if (node != null)
{
GetLastLL2(node._next, ref k);
if (k-- > 0)
Console.WriteLine(node._val);
}
}

- cristiscu April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Correct me if i'm wrong

public n, count;
void printFromTail(LList node)	{
	if(node == null)
		return;
	count ++;
	int index = count;
	printFromTail(node.next);
	if((count - index) < n)
		System.out.print(node.data + "->");
}

- jvj.pass April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int counter=0;
Public void printList(Node root, int k)
{
If(root!=null)
{
printList(root.next);
counter++;
if(counter==k)
Print root.data;
}
}

The above algorithm takes O(n) time.

- Prasad April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the code will print only the nth node from tail.

the condition count == k has to be changed to count <= k

- jvj.pass April 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printFromTail( Node root , int k ) {
         if(root== null ) throw new InvalidArgumentException();
         
         int[] mutable = int[1];
         mutable[0] = k; 
         printTreeFromTailCore(root, mutable) ; 
     }
     
     void printTreeFromTailCore(Node root , int[] mutable) {
     if(root == null || mutable == null ) return ; 
   
    printTreeFromTailCore(root.next, mutable) ; 
    
    mutable[0] -- ; 
   
    if(mutable[0] >= 0 ) {
       System.out.println(root.value) ; 
     } else {
       return ; 
     }
     }

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

We have 2 options here
1) Either reverse the linked list and print
2) Or put the list in a stack and then price the stack

- DashDash April 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void LastNrecurr(struct Node *node,int &n)
{
if(!node)
return;
LastNrecurr(node->next,n);
if(n)
{
printf("%d ",node->data);
n--;
}
}

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

public void getLastNElementFromLinkList(LinkedList<Integer> llist, int n) {
		LinkedList<Integer> templist = new LinkedList<>();
		it = llist.iterator();

		printLastNElement(llist, n);
	}

	void printLastNElement(LinkedList<Integer> llist, int m) {
		totalelement++;
		int elementcountiniteration = totalelement;
		if (it.hasNext()) {
			Integer element = (Integer) it.next();
			printLastNElement(llist, m);
			if ((totalelement - elementcountiniteration) <= 3) {
				System.out.println(element.intValue());
			}
		}
}

- Rudra April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printTail(int n, Node head){ 
	if(head==null) 
		return; 	
	Stack<Node> stack = new Stack<Node>(); 
	while(head!=null){ 
		stack.push(head); 
		head=head.next; 
	} 
	for(int i=0;i<n;i++) 
		System.out.println(stack.pop().value); 
}

- nimatohid April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printReverseNnodes (int n) {
		LinkedListNode previous=first, current=first, temp = first;
		
		for(int i=0; i<n+1; i++) {
		current=current.next;	
		}
		
		while (current.next!=null) {
			previous=previous.next;
			current=current.next;
		}
		
		temp=previous;
		
		while(temp.next!=null) {
			if(temp.next==current) {
				System.out.println("Node is "+current.iData);
				current=temp;
				temp=previous;
			}
			if (previous==current) {
				break;
			}
			temp=temp.next;
		}
			
	}

- Kumar April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printReverseNnodes (int n) {
		LinkedListNode previous=first, current=first, temp = first;
		
		for(int i=0; i<n+1; i++) {
		current=current.next;	
		}
		
		while (current.next!=null) {
			previous=previous.next;
			current=current.next;
		}
		
		temp=previous;
		
		while(temp.next!=null) {
			if(temp.next==current) {
				System.out.println("Node is "+current.iData);
				current=temp;
				temp=previous;
			}
			if (previous==current) {
				break;
			}
			temp=temp.next;
		}
			
	}

- Kumar April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution is independent of number of elements in linked list and has no dependency on value of 'n'

public void printReverseNnodes (int n) {
		LinkedListNode previous=first, current=first, temp = first;
		
		for(int i=0; i<n+1; i++) {
		current=current.next;	
		}
		
		while (current.next!=null) {
			previous=previous.next;
			current=current.next;
		}
		
		temp=previous;
		
		while(temp.next!=null) {
			if(temp.next==current) {
				System.out.println("Node is "+current.iData);
				current=temp;
				temp=previous;
			}
			if (previous==current) {
				break;
			}
			temp=temp.next;
		}
			
	}

- Kumar April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution is independent of number of elements in linked list and has no dependency on value of 'n'

public void printReverseNnodes (int n) {
		LinkedListNode previous=first, current=first, temp = first;
		
		for(int i=0; i<n+1; i++) {
		current=current.next;	
		}
		
		while (current.next!=null) {
			previous=previous.next;
			current=current.next;
		}
		
		temp=previous;
		
		while(temp.next!=null) {
			if(temp.next==current) {
				System.out.println("Node is "+current.iData);
				current=temp;
				temp=previous;
			}
			if (previous==current) {
				break;
			}
			temp=temp.next;
		}
			
	}

- Kumar April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution is independent of number of elements in linked list and has no dependency on value of 'n'


public void printReverseNnodes (int n) {
LinkedListNode previous=first, current=first, temp = first;

for(int i=0; i<n+1; i++) {
current=current.next;
}

while (current.next!=null) {
previous=previous.next;
current=current.next;
}

temp=previous;

while(temp.next!=null) {
if(temp.next==current) {
System.out.println("Node is "+current.iData);
current=temp;
temp=previous;
}
if (previous==current) {
break;
}
temp=temp.next;
}

}

- Kumar April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This solution is independent of number of elements in linked list and has no dependency on value of 'n'


public void printReverseNnodes (int n) {
LinkedListNode previous=first, current=first, temp = first;

for(int i=0; i<n+1; i++) {
current=current.next;
}

while (current.next!=null) {
previous=previous.next;
current=current.next;
}

temp=previous;

while(temp.next!=null) {
if(temp.next==current) {
System.out.println("Node is "+current.iData);
current=temp;
temp=previous;
}
if (previous==current) {
break;
}
temp=temp.next;
}

}

- Kumar April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

solution in ruby

def nodes_from_tail n
  n_list = LinkedList.new(1)
  2.upto(10) { |x| n_list.add(x) }
  reverse_list = n_list.list_to_reverse_arr
  new_list = LinkedList.new(reverse_list[0])
  1.upto(n-1) { |i| new_list.add(reverse_list[i]) }
  new_list.display
end

Where I have the following method added to my custom LinkedList class:

def list_to_reverse_arr
    current = @head
    full_list = []
    while current.next_node != nil
      full_list += [current.value.to_s]
      current = current.next_node
    end
    full_list += [current.value.to_s]
    full_list.reverse
  end

- VinceBarresi September 02, 2015 | 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