Rolling Median - Amazon Top Interview Questions
Problem Statement :
Implement a RollingMedian class with the following methods: add(int val) which adds val to the data structure median() which retrieves the current median of all numbers added Median of [1, 2, 3] is 2 whereas median of [1, 2, 3, 4] is 2.5. Constraints n ≤ 100,000 where n is the number of calls to add and median Example 1 Input methods = ["constructor", "add", "add", "add", "median", "add", "median"] arguments = [[], [1], [2], [3], [], [4], []]` Output [None, None, None, None, 2, None, 2.5] Explanation We first add 1, 2, and 3. The median is then 2. Then we add 4. Median is now (2 + 3) / 2 = 2.5
Solution :
Solution in C++ :
class RollingMedian {
public:
multiset<int> order;
multiset<int>::iterator it;
RollingMedian() {
}
void add(int val) {
order.insert(val);
if (order.size() == 1) {
it = order.begin();
} else {
if (val < *it and order.size() % 2 == 0) {
--it;
}
if (val >= *it and order.size() % 2 != 0) {
++it;
}
}
}
double median() {
if (order.size() % 2 != 0) {
return double(*it);
} else {
auto one = *it, two = *next(it);
return double(one + two) / 2.0;
}
}
};
Solution in Java :
import java.util.*;
class RollingMedian {
PriorityQueue<Integer> small, great;
public RollingMedian() {
this.small = new PriorityQueue<Integer>(Collections.reverseOrder());
this.great = new PriorityQueue<Integer>();
}
public void add(int val) {
if (this.small.size() == 0 && this.great.size() == 0) {
this.small.add(val);
} else if (this.small.size() > this.great.size()) {
if (this.small.peek() > val) {
this.great.add(this.small.poll());
this.small.add(val);
} else {
this.great.add(val);
}
} else {
if (this.small.peek() >= val) {
this.small.add(val);
} else {
this.great.add(val);
this.small.add(this.great.remove());
}
}
}
public double median() {
if (this.small.size() > this.great.size()) {
return this.small.peek();
} else {
return (double) (this.small.peek() + this.great.peek()) / 2;
}
}
}
Solution in Python :
class RollingMedian:
def __init__(self):
self.l = SortedList()
def add(self, val):
self.l.add(val)
def median(self):
if len(self.l) % 2 == 0:
return (self.l[len(self.l) // 2] + self.l[(len(self.l) // 2) - 1]) / 2
else:
return self.l[len(self.l) // 2]
View More Similar Problems
Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →