ORIGINAL CODING QUESTION
Agendas with limited nesting
A workshop agenda is a row of n opening markers and n closing markers. It is balanced when, reading left to right, the closings never outnumber the openings so far and both counts finish equal. Its depth is the largest number of markers that are open at the same moment. Count the balanced agendas with exactly n pairs whose depth is at most d.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
Two integers n d. Print the number of agendas.
Constraints
1 ≤ n ≤ 12; 1 ≤ d ≤ 12.
Example 1
Input
3 2
Output
4
Of the five balanced agendas with three pairs, only the fully nested one reaches depth 3.
Example 2
Input
4 1
Output
1
With depth 1 every opening must close immediately, giving one agenda.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Build the agenda one marker at a time and remember how many openings and closings are already placed.
Hint 2
You may open only while fewer than n openings are used and the current depth is below d; you may close only while something is open.
Hint 3
The number of completions depends only on (openings, closings), so memoize on that pair instead of listing every agenda.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →