Binary Search Tree Iterator Sequel - Facebook Top Interview Questions
Implement a binary search tree iterator with the following methods: next returns the next smallest element in the tree hasnext returns whether there is a next element in the iterator prev returns the next bigger element in the tree hasprev returns whether there is a previous element in the iterator For example, given the following tree root 4 / \ 2 7 / 5 Then we have it = BSTIterator(root) it.next() == 2 it.next() == 4 it.hasnext() == True i
View Solution →Common Reachable Node - Facebook Top Interview Questions
You are given a two-dimensional list of integers edges representing a directed graph and integers a and b. Each element in edges contains [u, v] meaning there's an edge from u to v. Return whether there's some node c such that we can go from c to a and c to b. Constraints 1 ≤ n ≤ 100,000 where n is the length of edges Example 1 Input edges = [ [0, 4], [4, 3], [1, 2], [0, 1], [0, 2], [1, 1] ] a = 2 b = 3 Output True
View Solution →Compressed Vector Product - Facebook Top Interview Questions
You are given two integer lists a and b where each list represents a vector in run-length encoded form. For example, a vector [1, 1, 1, 1, 2, 2, 2, 2, 2] is represented as [4, 1, 5, 2]. (There are 4 ones and 5 twos.) Return the dot product of the two vectors a and b. The dot product of a vector [x1, x2, ..., xn] and [y1, y2, ..., yn] is defined to be x1 * y1 + x2 * y2 + ... + xn * yn. Constraints 1 ≤ n ≤ 200,000 where n is the length of a 1 ≤ m ≤ 200,000 where m is the len
View Solution →Eat Bananas in K Hours - Facebook Top Interview Questions
You are given a list of integers piles and an integer k. piles[i] represents the number of bananas on pile i. On each hour, you can choose any pile and eat r number of bananas in that pile. If you pick a pile with fewer than r bananas, it still takes an hour to eat the pile. Return the minimum r required such that you can eat all the bananas in less than or equal to k hours. Constraints n ≤ 100,000 where n is the length of piles n ≤ k Example 1 Input piles = [6, 4, 3]
View Solution →Length of the Longest Path in an N-Ary Tree - Facebook Top Interview Questions
You are given a two-dimensional list of integers edges representing an n-ary tree. Each element in edges contains [u, v] meaning that u is the parent of v. Return the length of the longest path in the tree. Constraints 1 ≤ n ≤ 100,000 where n is the length of edges Example 1 Input edges = [ [1, 2], [1, 3], [1, 4], [4, 5] ] Output 4
View Solution →