Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

using System;
					
public class Program
{
	public static void Main()
	{
		Node n1 = new Node(9);
		Node n2 = new Node(9);
		Node n3 = new Node(9);
		
		n1.next = n2;
		n2.next = n3;
		
		
		Node n4 = new Node(1);
		Node n5 = new Node(2);
		Node n6 = new Node(3);
		
		n4.next = n5;
		n5.next = n6;
		
		Node nr = MultiPly (n1 , n4);
		Node cursor = nr;
		while(cursor != null)
			{
				Console.Write(cursor.data);
				cursor = cursor.next;
			}
	}
	public static Node MultiPly (Node head1 , Node head2)
	{
		Node number1 = Reverse(head1);
		Node number2 = Reverse(head2);
		
		int ret;
		int carryout = 0;
		Node result = new Node(0);
		Node reultHead = result;
		Node resultCur = result;
		while(number2 != null)
		{
			Node marker = number1;
			result = resultCur;
			carryout = 0;
			while(marker != null)
			{
				
			  //Console.WriteLine(carryout);
			   ret = (marker.data * number2.data);
			   result.data = ret + result.data + carryout;
				if (result.data > 9)
				{
					carryout = result.data/10;
					result.data = result.data%10;
					
				}	
				
				if ( result.next == null)
				{
					Node resultnext = new Node(0);
					result.next = resultnext;
				}
				//Console.WriteLine(marker.data + " * " + number2.data + "=" + result.data ) ;
				result = result.next;
				marker = marker.next;
			}
			if (carryout > 0)
			{
				result.data = carryout;
			}
			number2 = number2.next;
			resultCur = resultCur.next;
		}
		
		return Reverse(reultHead);
		
	}
	public static Node Reverse (Node head)
	{
		Node cur = head;
		Node prev = null;
		Node next = null;
		
			while (cur != null)
		{
			next = cur.next;
			cur.next = prev;
			prev = cur;
			cur = next;
		}
		return prev;
	}
	public class Node{
		
		
		public Node prev;
		public Node next;
		public int data ;
		public Node (int d)
		{
			this.data = d;
		}
	
	}
}

- mohamadreza.shakouri April 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there a condition for not having extra space or recursion ?

- Mohammad Reza Shakouri April 01, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Number 1 : 25, represented as [2] -> [5] -> null
Number 2: 5, represented as [5] -> null

Bogus solution could be to convert the linkedlist into strings, then parse it back into integers. I'm sure this isn't what they're looking for.

public int multiplyLLNumbers(LinkedList one, LinkedList 2)
{
	Node node = one.Head();
	String temp = "";
	while(onenode.next() != null)
	{
		temp += node.data.toString();
	}
	
	String temp2 = "";
	node = two.Head();
	while(two.next() != null)
	{
		temp2 += two.data.toString();
	}
	
	return (Integer.parseInt(temp1) * Integer.parseInt(temp2));
}

- parikksit.b March 08, 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