Count Recursive Merge Calls

You are required to simulate the recursive splitting process used by the Merge Sort algorithm.

You must implement a class MergeSortSimulator with a single method:

  • simulate(queries): Takes a vector<int> of Q queries, where each query is a length n. For each query, it simulates a recursive split function starting with length n.
  • The split(length) function increments a global total function call count upon entry and records the current active call-stack depth.
  • If length <= 1, it immediately returns.
  • Otherwise, it makes a recursive call to split(floor(length / 2)), waits for it to return, and then makes a second recursive call to split(length - floor(length / 2)).
  • The initial call split(n) begins at a depth of 1 (even if n = 0).

The method should return a vector<vector<int>>, where each inner vector corresponds to a query and contains exactly two integers: [total_call_count, maximum_active_depth].

Example 1:

Input : queries = [0, 1, 3]

Output: [[1, 1], [1, 1], [5, 3]]

Explanation:

for n=3:

split(3) (Depth 1, Calls: 1)

  • split(1) (Depth 2, Calls: 2) returns
  • split(2) (Depth 2, Calls: 3)
  • split(1) (Depth 3, Calls: 4) returns
  • split(1) (Depth 3, Calls: 5) returns

Total calls made: 5. Maximum depth reached: 3.

Example 2:

Input: queries = [4]

Output: [[7,3]]

Explanation:

for n=4:

split(4) (Depth 1, Calls: 1)

  • split(2) (Depth 2, Calls: 2)
  • split(1) (Depth 3, Calls: 3) returns
  • split(1) (Depth 3, Calls: 4) returns
  • split(2) (Depth 2, Calls: 5)
  • split(1) (Depth 3, Calls: 6) returns
  • split(1) (Depth 3, Calls: 7) returns

Total calls made: 7. Maximum depth reached: 3.

Now Your Turn!

Pick the correct output for the given input

Input: queries = [5]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= Q <= 1000 (Number of queries)
  • 0 <= n <= 105
  • The sum of n over all queries in a single test case will not exceed 5e6.
0
class MergeSortSimulator {
public:
vector<vector<int>> simulate(vector<int> queries) {
// Your code goes here
return {};
}
};
Test Case

Input:

Queries