Amazon Interview Question for Software Engineer / Developers






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

First build the table of letter -> frequency info.
Then build the Huffman tree, greedy select the two lowest cost trees and merge until you only have one left.

BuildHuffmanTree(Set<Letter> alphabet)
{
pq = new PriorityQueue<HuffmanNode>()
foreach (Letter l in alphabet)
  pq.insert(new HuffmanNode(l))
while (pq.size() > 1)
  HuffmanNode right = pq.pop_min
  HuffmanNode left = pq.pop_min
  pq.insert(new HuffmanNode(left, right))
return pq.pop_min
}

DFS will let you build the letter -> huffman code table.

BuildHuffmanTable(HuffmanNode node, List code)
  if (!node) return
  if (node.letter) {
    node.letter.code = new List(code);
    return
  }
  code.push(0)
  BuildHuffmanTable(node.left,code)
  code.pop()
  code.push(1)
  BuildHuffmanTable(node.right,code)
  code.pop()
}

Then the Letters in 'alphabet' all have their codes set.

- Anonymous October 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hope this code will help you. It will not used any additional packages.
-----------------------------------------------------

public class HuffmanTree {

	static int totalChars = 0;
	
	public static void main(String[] args) {
		//		String str = "abcdedaca";
		//		String str = "abcdedacafghiabcdefg";
		String str = "abcde";
		String  res = constructTreeWith(str);
		System.out.println("encrypted value ...");
		System.out.println(res);
	}

	public static String constructTreeWith(String str)
	{
		String encodeStr = "";
		Node[] nodes = new Node[1000];

		countChars(nodes, str);
		sortNodes(nodes);

		for(int i = 0; i < totalChars; i++)
		{
			System.out.println(nodes[i].ch + " = " + nodes[i].frequency);
		}
		createTree(nodes);

		for(int i = 0; i < totalChars; i++)
		{
			Node temp = nodes[i];
			nodes[i].tempCh = "";
			while(temp.parent != null)
			{
				nodes[i].tempCh = temp.nodeValue + nodes[i].tempCh ;
				temp=temp.parent;
			}
		}

		for(int i = 0; i < totalChars; i++)
		{
			System.out.println(nodes[i].ch + " = " + nodes[i].tempCh);
		}

		for(int j=0;j<str.length(); j++)
		{
			for(int i = 0; i < totalChars; i++)
			{
				if(nodes[i].ch == str.charAt(j))
				{
					encodeStr += nodes[i].tempCh;
					break;
				}
			}
		}

		return encodeStr;
	}

	public static void createTree(Node[] nodes)
	{
		int i = -1;

		Node p1 = nodes[++i];
		Node 	p2 = nodes[++i];
		Node p3 = nodes[++i];

		while(i < totalChars )
		{
			if(p1 != null && p3 != null &&  p1.frequency > p3.frequency)
			{
				Node tmp1 = new Node();
				tmp1.frequency = p2.frequency + p3.frequency;
				tmp1.tempCh = "Node :: " + p2.ch + p3.ch;;
				tmp1.nodeValue = 1;

				p2.nodeValue = 0;
				p2.parent = tmp1;
				p3.nodeValue = 1;
				p3.parent = tmp1;

				Node tmp2 = new Node();
				tmp2.frequency = p1.frequency + tmp1.frequency;
				tmp2.tempCh = "Node :: " + p1.ch + tmp1.ch;
				tmp2.nodeValue = 0;
				tmp1.parent = tmp2;
				p1.parent = tmp2;

				p1 = tmp2;
				p2 = nodes[++i]; 
				p3 = nodes[++i];
			}
			if( p1 != null && p2 != null)
			{
				Node tmp1 = new Node();
				tmp1.frequency = p1.frequency + p2.frequency;
				tmp1.tempCh = "Node :: " + p1.ch + p2.ch;
				tmp1.nodeValue = 0;

				p1.nodeValue = 0;
				p1.parent = tmp1;
				p2.nodeValue = 1;
				p2.parent = tmp1;

				p1=tmp1;
				p2=p3;
				p3=nodes[++i];
			}

		}

	}

	public static void countChars(Node[] nodes, String str)
	{
		for(int i = 0; i < str.length(); i++)
		{
			boolean isCharAvailable = false;
			for(int j = 0; j < totalChars -1; j++)
			{
				if(nodes[j].ch == str.charAt(i))
				{
					nodes[j].frequency += 1;
					isCharAvailable = true;
					break;
				}
			}
			if(!isCharAvailable)
			{
				nodes[totalChars] = new Node();
				nodes[totalChars].ch = str.charAt(i);
				nodes[totalChars].frequency = 1;
				totalChars+=1;
			}
		}
	}

	public static void sortNodes(Node[] nodes)
	{
		int j = 0;
		Node tmp = null;
		for(int i=0;i<totalChars;i++){
			j = i;
			for(int k = i;k<totalChars;k++){
				if(nodes[j].frequency>nodes[k].frequency){
					j = k;
				}
			}
			tmp = nodes[i];
			nodes[i] = nodes[j];
			nodes[j] = tmp;
		}
	}
}

class Node{
	char ch;
	int  nodeValue;
	int frequency;
	Node parent;
	String tempCh;

}

- Thirumaleshwar Kunamalla July 04, 2014 | 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