DSA interview question
A backup tool stores binary trees (with distinct values) as two traversal sequences instead of pointers, and the team must pick which two traversals to store so that the tree can always be rebuilt unambiguously. Given two traversal types, coded 1 = preorder, 2 = inorder and 3 = postorder, return whether they always determine a unique tree. The rule is short: the pair must include inorder and one other traversal. Preorder or postorder identify the root; inorder then splits everything else into left and right. Preorder with postorder can't tell a lone left child from a lone right child.
Example: (1, 2) → true; (1, 3) → false.
bool isPossible(int a, int b); // 1 = preorder, 2 = inorder, 3 = postorder
Input (a, b) | Output | Explanation |
|---|---|---|
(1, 2) | true | Preorder + inorder |
(2, 3) | true | Inorder + postorder |
(1, 3) | false | "1 with left child 2" and "1 with right child 2" give the same sequences |
(2, 2) | false | Inorder alone doesn't fix the shape |
(3, 1) | false | Order of the arguments doesn't matter |
(Confirm: node values are distinct; "unique" means for every tree, not just some trees.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in