Amazon Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




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

Here is how we will do this in two moves.

public static Node DeepCopy(Node root)
{
	if(root == null)
	{
		return null;
	}
	Node  currentNode = root;
	// this loop will create all the new nodes in the same input linkedlist.
	while(currentNode != null)
	{
		Node temp = new Node(currentNode.data);
		temp.next = currentNode.next;
		currentNode.next = temp;
		currentNode = temp.next;
	}
	currentNode = root;
	Node newList = currentNode.next;
	Node newCurrentNode = currentNode.next;
	while(currentNode != null)
	{
		newCurrentNode = currentNode.next;
		if(currentNode.random != null)
		{
			newCurrentNode.random = currentNode.random.next;
		}
		currentNode.next = newCurrentNode.next;
		currentNode = newCurrentNode.next;
		newCurrentNode.next = currentNode.next;
	}
	return newList;
}

Complexity is O(n) + O(1).

- sonesh April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I came up with below answer after modifying the Node class. The complexity is O(n) even though I might have to go through the entire list thrice. Correct me on complexity if I am wrong.

public class Node
{
	public int Value {get;set;}
	public Node Next{get;set;}
	public Node Random{get;set;}
	public int NodeCountFromHead {get;set;}
}

static Node DeepCopy(Node sourceHead)
{
	if(sourceHead == null)
		return null;
	
	int index = 1;
	sourceHead.NodeCountFromHead = index;
	
	Node sourceCurrent = sourceHead;
	Node destHead = new Node {Value = sourceHead.Value, Next = null};
	Node destCurrent = destHead;
	
	while(sourceCurrent.Next != null)
	{
		sourceCurrent.Next.NodeCountFromHead = ++index;
		destCurrent.Next = new Node{Value = sourceCurrent.Next.Value, Next = null, Random = null, NodeCountFromHead = index};
		sourceCurrent = sourceCurrent.Next;
		destCurrent = destCurrent.Next;
	}
	
	// second loop to set the Random node values
	sourceCurrent = sourceHead;
	destCurrent = destHead;
	while(sourceCurrent != null)
	{
		destCurrent.Random = GetNthNode(destHead, sourceCurrent.Random.NodeCountFromHead);
		sourceCurrent = sourceCurrent.Next;
		destCurrent = destCurrent.Next;
	}
	
	return destHead;
}

static Node GetNthNode(Node destHead, int nodeCountFromHead)
{
	Node current = destHead;
	
	int index = 1;
	while (index != nodeCountFromHead)
	{
		current = current.Next;
	} 
	
	return current;
}

- Abcd April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That's great. Thank you!

Only one question: O(n) makes sense. Where did you get O(1) from?

- Abcd April 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I generally write time and space complexity together. So O(n) is time and O(1) extra space apart from what we have to use.

- sonesh April 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use recursion for simplicity

static Node getDeepCopy(Node root){
		if(root == null) return null;
		
		Node newRoot = new Node(root.value);
		newRoot.next = getDeepCopy(root.next);
		newRoot.random = getDeepCopy(root.random);
		
		return newRoot;
	}

- King@Work June 05, 2017 | 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