TCS Digital Engineer Interview — Top-K Heap Under a Clock
- Field
- Engineering
- Company
- Tata Consultancy Services
- Role
- Digital Software Engineer Trainee
- Duration
- 20 min
- Difficulty
- Hard
- Completions
- New
- Updated
- 2026-06-11
How to prepare
What this round tests, what strong and weak answers sound like, and the traps to sidestep.
What this round is about
- Topic focus. Two heap and priority-queue problems on the canvas: the kth largest element of an unsorted array, then the running median of a stream of numbers.
- Conversation dynamic. The interviewer asks for your first idea, lets you propose sorting everything, then pushes on whether the whole array needs sorting to nudge you toward a heap of size k.
- What gets tested. Whether you state time and space complexity before coding, justify a min-heap over a max-heap, and reach for two heaps when you hear the word stream.
- Round format. A twenty-minute spoken drill with a shared whiteboard where you draw the heap as a tree and trace a small example.
What strong answers look like
- Brute force first, with a number. You open with sort descending and index to k, then say that costs order n log n, before writing anything.
- Pattern recognition. You hear kth largest and reach for a heap of size k at order n log k without being dragged there.
- Choice justified. You say a min-heap keeps the kth largest at the root so you can evict the smallest of your current k cheaply.
- Trace on the board. You draw the heap as a tree and dry-run five or six numbers, catching your own off-by-one.
What weak answers look like (and how to avoid them)
- Code before approach. Writing the loop before saying the plan or the complexity; say the approach and the cost out loud first.
- Heap named, not defended. Saying use a heap but not why a heap beats a sort; tie the choice to evicting the smallest cheaply.
- Sort the whole thing. Sorting the full array when only the k largest are needed; ask whether you need everything sorted.
- Silence under pushback. Going quiet when challenged; keep thinking out loud so the interviewer can follow.
Pre-interview checklist (2 minutes before you start)
- Recall the size-k trick. Push each element into a min-heap and pop when it grows past k, leaving the kth largest at the root.
- Have the two-heap median ready. A max-heap for the smaller half, a min-heap for the larger half, kept balanced.
- Pull up the whiteboard. Be ready to draw the heap as a tree, not just talk about it.
- Identify your baseline. Decide the brute-force sort cost for kth largest so you can state it immediately.
- Think of quickselect. Have the average linear time partition argument ready for the follow-up.
How the AI behaves
- Probes every answer. Asks for the complexity and the reason behind the data structure, not just the headline idea.
- No mid-interview praise. Will not say great answer; it acknowledges the specific point you made and pushes further.
- Interrupts on code-first. Pushes you back to the approach and the complexity if you start writing the loop too early.
- Escalates when you are clear. Brings up quickselect and the streaming median once you are reasoning out loud.
Common traps in this type of round
- Complexity stated wrong. Calling the heap-of-size-k solution order n log n instead of order n log k.
- Wrong heap polarity. Using a max-heap of size k for the k largest and making eviction of the smallest expensive.
- No invariant. Describing two heaps for the median without saying the sizes differ by at most one.
- Skipping the dry run. Declaring the code correct without tracing it on a small example.
- Quickselect worst case ignored. Claiming quickselect is always linear without noting the order n squared worst case unless the pivot is randomised.
- Stream cue missed. Re-sorting the whole history on every new number instead of maintaining the two heaps.
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.
- 1Kth Largest Element in an Array
You are given a large unsorted array of integers nums and an integer k. Return the kth largest element in the array. Note that it is the kth largest in sorted order, not the kth distinct element. Start by stating your brute-force approach and its complexity, then optimise. Do we need the whole array sorted just to know the kth largest?
Example inputnums = [3, 2, 1, 5, 6, 4], k = 2Example output5- 1 <= k <= length of nums <= 1,000,000
- Array values can repeat; the kth largest counts duplicates by position, not by distinct value
- State time and space complexity for every approach before coding
- On the whiteboard: draw the min-heap of size k as a tree, label the root, and trace one push and pop as a bigger element arrives
- 2Find Median From a Data Stream
Numbers arrive one at a time as a stream. After each new number, return the median of all numbers seen so far. The median is the middle value if the count is odd, or the average of the two middle values if the count is even. Keep the median cheap to read on every arrival without re-sorting the whole history.
Example inputaddNum(1), addNum(2) -> median 1.5, then addNum(3) -> median 2Example output1.5, then 2- Up to 1,000,000 numbers may stream in, so re-sorting on every arrival is too slow
- Insertion should be order log n and reading the median should be order one
- State the balancing invariant out loud before coding
- On the whiteboard: draw the max-heap for the smaller half and the min-heap for the larger half side by side, label both roots, and show which root gives the median
The full breakdown
How you're scored, the questions candidates ask most, and the research this interview is built on. Skim it — or just start the interview.
Interview framework
You will be scored on these 6 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.
- Brute-Force To Heap Optimisation Path20%
- Time And Space Complexity Accuracy20%
- Min Versus Max Heap Justification18%
- Whiteboard Trace Alignment15%
- Streaming Two-Heap Invariant15%
- Edge-Case And Correctness Discipline12%
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.
- Top 40 TCS Coding Interview Questions and Answers [2026]internshala.com
- Heap (Priority Queue) Interview Questions: Patterns and Strategies | CodeJeetcodejeet.com
- TCS NQT Preparation Sheet 2026: Aptitude, Verbal & Coding Questions - GeeksforGeeksgeeksforgeeks.org
- 12 Heap Interview Questions (SOLVED) For Your Next Coding Interview | FullStack.Cafefullstack.cafe