Algorithm Interview Questions | Glassdoor

Algorithm Interview Questions


interview questions shared by candidates

Algorithm Interview Questions

Sort: Relevance Popular Date

Find the second largest element in a Binary Search Tree

16 Answers

find the right most element. If this is a right node with no children, return its parent. if this is not, return the largest element of its left child.

One addition is the situation where the tree has no right branch (root is largest). In this special case, it does not have a parent. So it's better to keep track of parent and current pointers, if different, the original method by the candidate works well, if the same (which means the root situation), find the largest of its left branch.

if (root == null || (!root.hasRightChild() ) { return null;} else return findSecondGreatest(root, root.getValue()); value findSecondGreatest(Node curr, value oldValue) { if(curr.hasRightChild()) { return (findSecondGreatest( curr.getRightChild(), curr.value)); } else return oldValue; }

Suppose you have a matrix of numbers. How can you easily compute the sum of any rectangle (i.e. a range [row_start, row_end, col_start, col_end]) of those numbers? How would you code this?

6 Answers

If you had 5,623 participants in a tournament, how many games would need to be played to determine the winner

61 Answers

Given the daily values of a stock, find how you can lose the most with one buy-sell trading.

14 Answers

Number of 1's in binary representation of integer?

12 Answers

Given an array of integers where each element points to the index of the next element how would you detect if there is a cycle in this array?

15 Answers

Write a function Brackets(int n) that prints all combinations of well-formed brackets. For Brackets(3) the output would be ((())) (()()) (())() ()(()) ()()()

11 Answers

Given an array of integers eg [1,2,-3,1] find whether there is a sub-sequence that sums to 0 and return it (eg 1,2,-3 or 2,-3,1) Checking every sub-sequence is O(n^2) which is too inefficient

12 Answers

Implement a power function to raise a double to an int power, including negative powers.

11 Answers

Implement a function to validate whether a given binary tree is a BST (i.e. write an isBST() function).

9 Answers
110 of 786 Interview Questions