Amazon Interview Question for SDE1s


Country: United States




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

public class frequency {
	public static int[] digitFreq = int[10];

	public static void treeFreq (BTNode tree) {
		/* Base Case */
		if (tree == null) {
			return;
		}

		/* Visit Node, check digit frequency */
		int number = tree.data;
		do {
			digitFreq[number % 10]++;
			number /= 10;
		} (while number != 0);

		treeFreq(tree.left);	/* Check left sub-tree */
		treeFreq(tree.right);	/* Check right sub-tree */
	}
}

public class BTNode {
	int data;
	BTNode left;
	BTNode right;

	public BTNode {
		data = null;
		left = null;
		right = null;
	}
}

- gsheld January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry found a bug:

public static int[] digitFreq = new int[10];

- gsheld January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I also thought of same procedure of maintaining an auxiliary space of 10 digits and then taking each digit of each element in BST.

Time Complexity: O(Summation of digits of all elements in BST)
Space Complexity: O(10) // Since there are 10 digits

Edit:
(1). Whether the input numbers are given in form BST, unsorted array or anything else complexity can't be reduced further.
(2). Please some one explain essence of BST in this question.

- codeAddict January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hrmm... My first thought was just using a hashtable or make your own data structure.
Do an ___-order traversal and every time you get to a node, look at the digit, see if your data structure has it:
a. Has it? K, increment the count of it.
b. Doesn't have it? First time it's come up so start it at zero.

For each digit we use our modified version of put/add. O(n)?

- smd January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class BTreePreOrder {

	private Node root;
	private Map<String, Integer> frequencyCounter = null;

	private void treeTravelAndeFindFrequency(Node root) {
		if (root == null)
			return;
		findFrequency(root.getNodeValue());
		treeTravelAndeFindFrequency(root.getLeftNode());
		treeTravelAndeFindFrequency(root.getRightNode());
	}

	private void findFrequency(int value) {
		String valueString = "" + value;
		for (char ch : valueString.toCharArray()) {
			Integer intValue = frequencyCounter.get(""+ch);
			if (intValue != null)
				frequencyCounter.put(""+ch, ++intValue);
			else
				frequencyCounter.put(""+ch, 1);
		}
	}
	
	public void add(int array[]){
		for (int value : array) {
			add(new Node(value));
		}
	}

	public void add(Node node) {
		Node x, y;
		// Assume root is initialized to null
		x = y = root;

		while (x != null) {
			if (x.getNodeValue() > node.getNodeValue()) {
				y = x;
				x = x.getLeftNode();
			} else {
				y = x;
				x = x.getRightNode();
			}
			
		}

		// y will be the parent of node
		if (y == null) {
			root = node;
			return;
		}

		if (y.getNodeValue() > node.getNodeValue())
			y.setLeftNode(node);
		else
			y.setRightNode(node);

		node.setParent(y);
	}

	public void getFrequencyOfEachDigit() {
		frequencyCounter = new HashMap<String, Integer>();
		treeTravelAndeFindFrequency(root);
		for (Entry<String, Integer> entrySet : frequencyCounter.entrySet()) {
			System.out.println(entrySet.getKey() + " " + entrySet.getValue());
		}
	}
	
	class Node {
		private Node rightNode;
		private Node leftNode;
		private Node parent;
		private int nodeValue;
		
		public Node(int nodeValue){
			this.nodeValue = nodeValue;
		}
		
		
		/**
		 * @return the parent
		 */
		public Node getParent() {
			return parent;
		}


		/**
		 * @param parent the parent to set
		 */
		public void setParent(Node parent) {
			this.parent = parent;
		}
		
		
		/**
		 * @return the rightNode
		 */
		public Node getRightNode() {
			return rightNode;
		}


		/**
		 * @param rightNode the rightNode to set
		 */
		public void setRightNode(Node rightNode) {
			this.rightNode = rightNode;
		}


		/**
		 * @return the leftNode
		 */
		public Node getLeftNode() {
			return leftNode;
		}


		/**
		 * @param leftNode the leftNode to set
		 */
		public void setLeftNode(Node leftNode) {
			this.leftNode = leftNode;
		}


		/**
		 * @param nodeValue the nodeValue to set
		 */
		public void setNodeValue(int nodeValue) {
			this.nodeValue = nodeValue;
		}


		/**
		 * @return nodeValue
		 */
		public int getNodeValue() {
			return nodeValue;
		}
	}

	public static void main(String[] args) {
		int[] array = { 1, 2, 9, 12, 123, 123, 4 };
		BTreePreOrder btree = new BTreePreOrder();
		btree.add(array);
		btree.getFrequencyOfEachDigit();
	}
}

- Django January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A binary Search tree is an ordered binary tree with *NO* duplicates. So, every digit will occur only once in the tree. So, frequency is 1/(total nodes in the tree). So, the problem, to me, boils down to counting the total number of nodes in the tree.

Am I missing anything people? Lemme know please. Thanks.

- smallchallenges January 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

It's true that the BSD has no duplicated *values*. However the *digits* can repeat themselves. e.g. 1 and 11 are different values but they share the same digits so the count of the digit 1 is 3

- Moran January 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

call this method in any ***order traversal :

Map<Integer,Integer> hash = new HashMap<Integer,Integer>();
public void findFrequency(int data){
if(hash.containsKey(data)){
Integer temp = (Integer) hash.get(data);
temp++;
hash.put((Integer)data,temp);
}
else{
hash.put((Integer)data, 1);
}
}

- asees January 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey Brother,


Best thing I have read in a while on this Amazon Interview Question for SDE1s. There should be a standing ovation button. This is a great piece.


When creating a crawler that reads from S3, it would be nice if you could specify the Athena table name. At the moment it takes the name of the S3 folder it crawls.



By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).


Shukran,
Ajeeth

- Ajeeth June 04, 2018 | 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