Amazon Interview Questions With Solutions Java
Amazon Interview Questions With Solutions Java
Longest Substring
Given a sorted circularly linked list of Nodes that store integers and a new Node, insert the
new Node into the correct position. (Duplicates allowed)
How many times will a robot need to retrieve bins if it is given an array of bin ID's and it can
only hold N bins at a time? When the robot is already holding N bins, it will return the least
recently retrieved bin and store the new bin.
Given an ArrayList of Nodes, with each Node having an ID and a parent ID, determine
whether the List is given in preorder.
find-all-anagrams-in-a-string
nodes.add(root);
values.add(root.val);
while(!nodes.isEmpty()){
TreeNode curr = nodes.poll();
int sumValue = values.poll();
if(curr.left != null){
nodes.add(curr.left);
values.add(sumValue+curr.left.val);
}
if(curr.right != null){
nodes.add(curr.right);
values.add(sumValue+curr.right.val);
}
}
return false;
}
}
Given a list of weighted edges between nodes, find the minimum cost spanning tree
Given the upper left and lower right coordinates of two rectangles, determine if they overlap
fizz buzz and variations on it.
Implementing queue with stack.
private int front;
public void push(int x) {
if (s1.empty())
front = x;
while (!s1.isEmpty())
s2.push(s1.pop());
s2.push(x);
while (!s2.isEmpty())
s1.push(s2.pop());
}
public void pop() {
s1.pop();
if (!s1.empty())
front = s1.peek();
}
public boolean empty() {
return s1.isEmpty();
}
public int peek() {
return front;
}
//using two stacks
private Stack<Integer> s1 = new Stack<>();
private Stack<Integer> s2 = new Stack<>();
// Push element x to the back of queue.
public void push(int x) {
if (s1.empty())
front = x;
s1.push(x);
}
public void pop() {
if (s2.isEmpty()) {
while (!s1.isEmpty())
s2.push(s1.pop());
}
s2.pop();
}
public boolean empty() {
return s1.isEmpty() && s2.isEmpty();
}
public int peek() {
if (!s2.isEmpty()) {
return s2.peek();
}
return front;
}
finding intersection of 2 lists.
Find the longest palindromic substring.
public String longestPalindrome(String s) {
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return s.substring(start, end + 1);
}
Given an array input of N number of strings. [ cat, tac, pot, top, meow, act ]
Return the output : [[act,cat,act], [pot,top], [meow]]
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
if (strs.length == 0) return new ArrayList();
Map<String, List> ans = new HashMap<String, List>();
for (String s : strs) {
char[] ca = s.toCharArray();
Arrays.sort(ca);
String key = String.valueOf(ca);
if (!ans.containsKey(key)) ans.put(key, new ArrayList());
ans.get(key).add(s);
}
return new ArrayList(ans.values());
}
}
Write a function that returns true if a string of parentheses is balanced or not, such as "(())()", which
would return true. 3 Answers
private static boolean checkBalancedParentheses(String input){
Stack<String> stack = new Stack<String>();
boolean isBalanaced = false;
for(int i=0; i < input.length(); i++){
String str = ""+input.charAt(i); //store characters as
String
//if opening bracket then push into stack
if(str.equals("(") || str.equals("[") || str.equals("{")){
stack.push(str);
}
//if closing bracket, pop bracket and compare if its a pair
if(str.equals(")") || str.equals("]") || str.equals("}")){
//if stack becomes empty in between then also its not
balanced
if(stack.isEmpty()){
return false;
}
String opening = stack.peek();
if(str.equals(")") && opening.equals("(")){
stack.pop();
}
if(str.equals("]") && opening.equals("[")){
stack.pop();
}
if(str.equals("}") && opening.equals("{")){
stack.pop();
}
}
}
//if stack is empty at end, then its balanced
if(input.length() > 0 && stack.isEmpty()){
isBalanaced = true;
}
return isBalanaced;
}
Write a function that rotates a 2-dimensional array clockwise or counter clockwise 90 degrees
depending on a given parameter, which I believe was either -1 or 1, which told you which way to
rotate it. You are given the 2D array as a parameter as well. 2 Answers
for (int layer = 0; layer < arr.length/2; layer++) {
int first = layer;
int last = arr.length - layer - 1;
for (int i = first; i < last; i++) {
int offset = i - first;
//save top
int top = arr[first][i];
//top -> left
arr[first][i] = arr[last-offset][first];
//left -> bottom
arr[last-offset][first] = arr[last][last-offset];
//bottom -> right
arr[last][last-offset] = arr[i][last];
//right -> top
arr[i][last] = top;
}
}
Given a sorted array of integers in increasing order (can contain duplicates), return the last index of a
specified target integer, or return -1 otherwise. Thus 1, 2, 2, 3, 4 and the target is 2, the function
should return 2. 3 Answers
Use Binary Search
The final question was given a binary tree, find the max height of the tree. I solved this using
recursion and he wanted to know how I would do it without recursion
public int findHeight(TreeNode root) {
if(root == null){
return 0;
}
int leftHeight = 0;
if(root.left != null) {
leftHeight = findHeight(root.left);
}
int rightHeight = 0;
if(root.right != null){
rightHeight = findHeight(root.right);
}
return Math.max(leftHeight+1, rightHeight+1);
}
Design a function that reverses the second half of a singly-linked list Answer Question
public static ListNode reverse(ListNode start)
{
int counter = 0;
ListNode node = start;
ListNode pre = start;
ListNode result = start;
while (node!= null)// for count how many elements in linked list
{
counter += 1;
node = node.next;
}
for (int i=0; i< (counter / 2) ; i++)//no matter counter is even or odd, when it divided by 2, the result is
even
{
pre = start;
start = start.next;
}
ListNode temp = null;
ListNode preNext = null;// this variable is used to track the next val behind pre
// for example, 2->1->3->4->5->6->7->8
// at this moment, pre:4, start:5
// I treated 5->6->7->8 as an independent linkedlist
// I reversed the linkedlist
// Finally, set the pre node's next value to the reversed linkedlist's head
// The first half and second half have been connected together
while (start != null)
{
temp = start.next;
start.next = preNext;
preNext = start;
start = temp;
}
pre.next = preNext;
return start;
}
Design a function that finds a tree path with the lowest total value.
private static void path(Node root, ArrayList<Integer> list,int s) {
if(root==null) {
return;
} else {
list.add(root.info);
s = s+root.info;
}
if ((root.left == null && root.right == null)) {
System.out.println(list);
if(maxSum>s) {
maxSum = s;
finalList = new ArrayList<>(list);
}
return;
}
path(root.left, new ArrayList<Integer>(list),s);
path(root.right, new ArrayList<Integer>(list),s);
}