TCS Ninja Coding Interview — Recursion Tree and Binary Search Trace
- Field
- Engineering
- Company
- Tata Consultancy Services
- Role
- Assistant System Engineer Trainee (Ninja)
- Duration
- 20 min
- Difficulty
- Medium
- Completions
- New
- Updated
- 2026-06-09
What this round is about
- Topic focus. Two whiteboard problems, one on binary search for the first and last occurrence in a sorted array with duplicates, and one on recursion with factorial and Fibonacci.
- Conversation dynamic. A TCS panellist makes you talk while you code, state the base case before the recursive call, and trace the algorithm on a concrete input rather than narrating in the air.
- What gets tested. Whether you can write correct foundational code, draw the recursion tree, move the binary search pointers, and give both time and recursion-stack space complexity.
- Round format. A twenty-minute live coding round with a shared whiteboard, problems shown on cards, conducted at the foundational searching, sorting, and recursion altitude.
What strong answers look like
- Base case first. You say factorial of zero or one returns one before you write the recursive call, and you point at that base case on the board.
- Grounded logarithmic reasoning. You move left, right, and mid on a real array and explain that the search space halves each step, so it is order log n, instead of just reciting the number.
- Recursion tree drawn. For Fibonacci you draw the tree for n equals five, point at the repeated subtrees, and explain why recomputing them makes it exponential.
- Complexity stated unprompted. You finish each problem by stating time complexity and recursion-stack space, including order n stack depth for the recursive version.
What weak answers look like (and how to avoid them)
- Recursive call before base case. Writing the self-call with no stated stopping condition. Say the base case out loud first, every time.
- Untraced code. Writing the function and never running it on a sample input, so off-by-one and pivot errors hide. Dry-run a small input on the board before you claim it works.
- Silent on complexity. Going quiet when asked the time or space cost. Always volunteer both, and include the call-stack space for recursion.
- Recited log n. Saying order log n with no reason. Tie it to the search space halving at each midpoint.
Pre-interview checklist (2 minutes before you start)
- Recall the factorial base case. Have factorial of zero or one returns one ready to say before any recursive code.
- Have a binary search template in mind. Know your left, right, and mid update and which way you move when the value equals the target for first versus last occurrence.
- Think of the Fibonacci tree. Be ready to draw fib of five branching into fib of four and fib of three and point at the repeats.
- Identify your complexity lines. Be ready to state time and recursion-stack space for both the recursive and the iterative version.
- Pull up the whiteboard early. Plan to put pointers and boxes on the page in the first couple of turns rather than narrating in the air.
- Re-read the array. Confirm whether the input is sorted and whether it has duplicates before you choose your approach.
How the AI behaves
- Probes every answer. It asks at least one follow-up on each problem, pushing for the base case, the trace, or the complexity you left out.
- No mid-round praise. It will not say great answer or validate you, it acknowledges the specific thing you said and then pushes deeper.
- Redirects to the board. If you narrate without drawing, it tells you to pull up the whiteboard and trace it within the first couple of turns.
- Pressure-tests memorised answers. If you recite a complexity, it makes you prove why on the board rather than accepting the number.
Common traps in this type of round
- Missing base case. Starting the recursion with no stated stopping condition, then being unable to explain the stack overflow.
- Diagram-verbal mismatch. Saying you move right when the board shows the pointer going left, or a tree that does not match your spoken walkthrough.
- Proxy complexity. Claiming order log n or order n without grounding it in the halving or the stack depth.
- Naive Fibonacci with no insight. Writing the exponential version and not recognising the overlapping subproblems that cause it.
- Iterative versus recursive dodge. Refusing to pick one and say why you would use it in real code.
- Going silent under nerves. Solving in your head with no narration, leaving nothing for the interviewer to follow or verify.
Sample problems you'll face
The 2 problems below are the same ones you'll work through in the live session — no surprises. Read the constraints carefully; the AI persona will refer you to the on-canvas card by problem number.
- 1First and Last Occurrence in a Sorted Array
Given a sorted array of integers that may contain duplicate values, and a target value, find the index of the first occurrence and the index of the last occurrence of the target. If the target is not present, return -1 for both. Use binary search so the solution runs in logarithmic time, and be ready to trace the left, right, and mid pointers on a sample array.
Example inputarr = [1, 1, 2, 2, 2, 3, 4], target = 2Example outputfirst = 2, last = 4- The array is sorted in non-decreasing order and may contain duplicates.
- Target time complexity is O(log n); a linear scan is not accepted.
- State which direction you move the pointers when arr[mid] equals the target, separately for the first and the last occurrence.
- On the whiteboard, draw the array and mark left, right, and mid at each step, showing how the search space halves until the first and last positions are found.
- 2Factorial and Fibonacci with the Recursion Tree
Write a recursive function for factorial of n. Then write a recursive function for the nth Fibonacci number. State the base case before you write the recursive call in each. For Fibonacci, draw the recursion tree for n = 5, mark the base cases, and explain why the naive recursive version recomputes work. Give the time complexity and the recursion-stack space for each function, and say what causes a stack overflow.
Example inputfactorial: n = 4 fibonacci: n = 5Example outputfactorial(4) = 24 fibonacci(5) = 5- State the base case explicitly before writing any recursive call: factorial of 0 or 1 returns 1; fib(0) = 0 and fib(1) = 1.
- Give time complexity and recursion-stack space separately; note the recursive call stack uses O(n) auxiliary space.
- Explain what causes a stack overflow if the base case is never reached.
- Draw the recursion tree for n = 5 on the whiteboard, mark the base case, and circle at least one repeated subproblem such as fib(2).
Interview framework
You will be scored on these 5 dimensions. The full rubric with definitions is below.
What we evaluate
Your final scorecard breaks down across these dimensions. The full rubric and tier criteria are revealed inside the interview itself.
- Recursion Base Case Discipline20%
- Binary Search Trace Correctness18%
- Complexity Articulation and Grounding18%
- Recursion Tree and Subproblem Insight16%
- Iterative versus Recursive Judgment14%
- Out Loud Reasoning Narration14%
Common questions
Sources this interview is built on
Real candidate-report URLs (Glassdoor / AmbitionBox / PrepInsta / GeeksforGeeks / Medium) reviewed when authoring the questions, persona, and rubric. Verify the realism yourself.
- TCS NQT Coding Questions and Answers 2026 | PrepInstaprepinsta.com
- TCS Ninja Final Interview (All India Campus) - GeeksforGeeksgeeksforgeeks.org
- First and Last Occurrences in Array - takeuforwardtakeuforward.org
- Time complexity of recursive Fibonacci program - GeeksforGeeksgeeksforgeeks.org
- Difference between Recursion and Iteration - GeeksforGeeksgeeksforgeeks.org