ORIGINAL CODING QUESTION
Validate nested actions
Positive IDs open actions; negative IDs close the matching positive ID. Actions must close in reverse opening order. Print 1 if the entire sequence is correctly nested and fully closed, otherwise 0.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
n, followed by n nonzero signed action IDs. Print 1 or 0.
Constraints
1 ≤ n ≤ 10,000; 1 ≤ absolute action ID ≤ 100,000.
Example 1
Input
4 1 2 -2 -1
Output
1
Action 2 closes before action 1.
Example 2
Input
4 1 2 -1 -2
Output
0
Action 1 closes while action 2 is still open.
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
The most recently opened action must close first.
Hint 2
Push positive IDs; compare a negative ID against the top of the stack.
Hint 3
Reject mismatches and premature closes; finally require an empty stack.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →