Amazon Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

use a balanced binary search tree, similar to geeksforgeeks.org/count-smaller-elements-on-right-side

- confused_coder July 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SmallerOnRight {
	public static void updateCountSmaller(int[] elements, int[] smallerCount) {
		BSTNode root = new BSTNode(elements[0], 0);

		for (int i = 1; i < elements.length; i++) {
			BSTNode parent = null;
			BSTNode temp = root;
			while (temp != null) {
				if (temp.getData() > elements[i]) {
					parent = temp;
					updateSmallerCount(parent, smallerCount);
					temp = temp.getLeft();
					if (temp == null) {
						parent.setLeft(new BSTNode(elements[i], i));
					}
				} else {
					parent = temp;
					temp = temp.getRight();
					if (temp == null) {
						parent.setRight(new BSTNode(elements[i], i));
					}

				}
			}

		}
	}

	public static void updateSmallerCount(BSTNode parent, int[] smallerCount) {
		BSTNode runner = parent;
		while (runner != null) {
			smallerCount[runner.getIndex()] = smallerCount[runner.getIndex()] + 1;
			runner = runner.getRight();
		}

	}

	public static void main(String[] args) {
		int[] elements = new int[] { 12, 1, 2, 3, 0, 11, 4 };
		int[] smallerCount = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		updateCountSmaller(elements, smallerCount);
	}

}

- chestnut304 August 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why can't I just sort the array and do arraysize-current_element_index ? we can do it in o(N.logN).

- Sujith September 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.


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