Rank calculator
Difficulty: HARDCategory: DSAFrequency: 1Asked On 2025-05-01
Problem Statement
You are given a stream of integers. After each number is inserted, output the rank of the latest number in the stream.
The rank of a number is defined as the number of elements strictly less than it that have appeared so far in the stream.
(If you want to include the number itself, say so, and we’ll change it to “less than or equal to.”)
Example
Input:
1Stream: [5, 1, 4, 4, 6, 2]Output:
10 // [5] → 5 has 0 elements less than it
20 // [5, 1] → 1 has 0 elements less than it
31 // [5, 1, 4] → 4 has 1 element less than it (1)
41 // [5, 1, 4, 4] → 4 has 1 element less than it (1)
54 // [5, 1, 4, 4, 6] → 6 has 4 elements less than it (1, 4, 4, 5)
61 // [5, 1, 4, 4, 6, 2] → 2 has 1 element less than it (1)Final Output:
10
20
31
41
54
61Solution
c++ code:
1#include <iostream>
2using namespace std;
3
4int max(int val1, int val2){
5 return val1 > val2 ? val1 : val2;
6}
7
8class Node {
9 public:
10 Node(int val){
11 this->val = val;
12 this->dup_count = 1; // duplicates of this value
13 this->count = 1; // total nodes including duplicates in subtree
14 this->left = this->right = nullptr;
15 this->height = 1;
16 }
17
18 Node* left;
19 Node* right;
20 int count;
21 int val;
22 int dup_count;
23 int height;
24};
25
26class Tree {
27 private:
28 Node* root = nullptr;
29
30 public:
31 int get_height(Node* node){
32 return node == nullptr ? 0 : node->height;
33 }
34
35 int get_balance(Node* node){
36 return node == nullptr ? 0 : get_height(node->left) - get_height(node->right);
37 }
38
39 int get_count(Node* node){
40 return node == nullptr ? 0 : node->count;
41 }
42
43 Node* left_rotate(Node* x){
44 Node* y = x->right;
45 Node* T2 = y->left;
46
47 y->left = x;
48 x->right = T2;
49
50 x->height = 1 + max(get_height(x->left), get_height(x->right));
51 y->height = 1 + max(get_height(y->left), get_height(y->right));
52
53 x->count = x->dup_count + get_count(x->left) + get_count(x->right);
54 y->count = y->dup_count + get_count(y->left) + get_count(y->right);
55
56 return y;
57 }
58
59 Node* right_rotate(Node* y){
60 Node* x = y->left;
61 Node* T2 = x->right;
62
63 x->right = y;
64 y->left = T2;
65
66 y->height = 1 + max(get_height(y->left), get_height(y->right));
67 x->height = 1 + max(get_height(x->left), get_height(x->right));
68
69 y->count = y->dup_count + get_count(y->left) + get_count(y->right);
70 x->count = x->dup_count + get_count(x->left) + get_count(x->right);
71
72 return x;
73 }
74
75 Node* insert(int key){
76 root = insert(root, key);
77 return root;
78 }
79
80 Node* insert(Node* node, int key){
81 if(node == nullptr)
82 return new Node(key);
83
84 if(key < node->val){
85 node->left = insert(node->left, key);
86 } else if(key > node->val){
87 node->right = insert(node->right, key);
88 } else {
89 node->dup_count++;
90 node->count++; // just increase count, no height change needed
91 return node;
92 }
93
94 node->height = 1 + max(get_height(node->left), get_height(node->right));
95 node->count = node->dup_count + get_count(node->left) + get_count(node->right);
96
97 int balance = get_balance(node);
98
99 if(balance > 1 && key < node->left->val)
100 return right_rotate(node);
101 if(balance < -1 && key > node->right->val)
102 return left_rotate(node);
103 if(balance > 1 && key > node->left->val){
104 node->left = left_rotate(node->left);
105 return right_rotate(node);
106 }
107 if(balance < -1 && key < node->right->val){
108 node->right = right_rotate(node->right);
109 return left_rotate(node);
110 }
111
112 return node;
113 }
114
115 int get_rank(int key){
116 return get_rank(root, key);
117 }
118
119 int get_rank(Node* node, int key){
120 if(node == nullptr) return 0;
121
122 if(key < node->val){
123 return get_rank(node->left, key);
124 } else if(key > node->val){
125 return get_count(node->left) + node->dup_count + get_rank(node->right, key);
126 } else {
127 return get_count(node->left); // elements strictly less than key
128 }
129 }
130};
131
132int main() {
133 Tree* tree = new Tree();
134
135 tree->insert(5);
136 tree->insert(2);
137 tree->insert(6);
138 tree->insert(4);
139 tree->insert(1);
140 tree->insert(9);
141 tree->insert(2); // duplicate
142 tree->insert(5); // duplicate
143 tree->insert(5); // duplicate
144
145 cout << "Rank(1): " << tree->get_rank(1) << endl; // 0
146 cout << "Rank(2): " << tree->get_rank(2) << endl; // 1
147 cout << "Rank(4): " << tree->get_rank(4) << endl; // 3
148 cout << "Rank(5): " << tree->get_rank(5) << endl; // 4
149 cout << "Rank(6): " << tree->get_rank(6) << endl; // 7
150 cout << "Rank(9): " << tree->get_rank(9) << endl; // 8
151
152 return 0;
153}
154