TCS Ninja Number-Theory and Math Coding Drill
- Field
- Engineering
- Company
- Tata Consultancy Services
- Role
- Assistant System Engineer Trainee (Ninja)
- Duration
- 20 min
- Difficulty
- Medium
- 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
This is a timed coding drill that mirrors the TCS NQT coding section and the live write-a-program ask in the Ninja technical interview. You will write two small programs at the whiteboard: a prime check using trial division up to the square root of N, and the GCD of two numbers by the Euclidean algorithm, then extend it to the LCM and to listing all primes up to N with the Sieve of Eratosthenes. A TCS panellist, Arvind, will make you dry run your code on a sample input, state the time complexity, handle the edge cases, and answer one optimise-it follow-up per problem. The whole point is to make the most common NQT number-theory archetype, turning a number rule into correct compiling logic under a clock, feel automatic.
What strong answers look like
Strong candidates write the loop and immediately bound the prime check at the square root of N, explaining that any divisor larger than the square root pairs with a smaller one already tested. They pick a concrete number such as 37 and trace each divisor out loud, then state O of square root of N. On the math problem they state the GCD stopping condition before the recursive call, trace the remainder steps for 12 and 18, derive LCM as a times b divided by the GCD while guarding against overflow, and choose the Sieve over repeated prime checks for large N, naming O of N log log N. Above all they narrate their thinking the whole way through.
What weak answers look like (and how to avoid them)
Weak answers loop all the way to N without justifying any bound, treat 1 or 0 as prime, or declare the code correct without ever tracing it. On the math side they write a recursive call with no stated stopping condition, get the LCM formula wrong, or would run a separate prime check on every number even for a huge N. The most damaging habit is going silent and producing only finished code. Avoid all of this by stating your plan first, dry running on a real input before you declare done, naming your edge cases up front, and keeping a running commentary so the panellist can follow your reasoning.
Pre-interview checklist (2 minutes before you start)
- Recall the prime-check template: handle 0, 1 and negatives first, then loop a divisor from 2 up to the square root of N.
- Recall the Euclidean GCD recurrence and its stopping condition, and the LCM as a times b divided by GCD with divide-before-multiply.
- Recall the Sieve of Eratosthenes idea: mark multiples of each prime as composite, cost O of N log log N.
- Pick the language whose syntax you can write without hesitation, C, C++, Java or Python.
- Plan to think out loud and to dry run every program on a small sample input before saying you are done.
How the AI behaves
Arvind is a warm but exacting TCS systems engineer and campus panellist with nine years at the company. He states the first problem on turn one, makes you dry run your code on a concrete input before accepting it, always asks for the time complexity, and pushes specifically on why trial division stops at the square root of N. He probes edge cases on purpose, N equal to 0 or 1, negatives, and overflow, and asks exactly one optimise-it follow-up per problem. He nudges you to narrate when you fall silent, gives brief genuine acknowledgement when you are right, and never solves the problem for you. He softens in the last few minutes for a reflection and an encouraging close.
Common traps in this type of round
The classic traps are looping to N instead of the square root, treating 1 as prime, multiplying a times b before dividing by the GCD and overflowing, writing a recursive GCD with no base case, and reaching for a per-number prime check when the Sieve is the right tool for all primes up to N. The behavioural traps are just as costly: declaring code correct without a dry run, and going silent under pressure so the panellist cannot follow your reasoning. Name your edge cases, trace on a real input, and keep talking.
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.
- 1Check Whether a Number Is Prime (Trial Division to sqrt N)
Write a function that takes an integer N and returns whether N is prime. A prime number is greater than 1 and has no positive divisors other than 1 and itself. Use trial division but only test divisors up to the square root of N, not up to N. Be ready to dry run your function on a sample input on the whiteboard, state the time complexity, and explain why stopping at the square root of N is enough. Then handle the edge cases for N equal to 0, N equal to 1, and negative input.
Example inputN = 37Example outputtrue (37 is prime)- N can be 0, 1, a negative number, or a large positive integer; treat 0, 1, and negatives as not prime.
- Target time complexity is O(square root of N); a loop up to N is not accepted.
- State out loud why a divisor larger than the square root of N must pair with a smaller divisor you already checked.
- On the whiteboard, write the loop and dry run it for N = 37, showing each divisor you test from 2 up to the square root of 37 and why the function returns prime.
- 2GCD by Euclid, then LCM, then Primes up to N by the Sieve
Write a function that returns the GCD of two positive integers a and b using the Euclidean algorithm, then use it to compute their LCM as a times b divided by the GCD. State the base case of the recursion or the stopping condition of the loop before you write the recursive call. Then sketch how you would print all primes up to N with the Sieve of Eratosthenes instead of running a prime check on every number, and give the time complexity of each approach.
Example inputa = 12, b = 18 sieve: N = 20Example outputgcd = 6, lcm = 36 primes up to 20: 2 3 5 7 11 13 17 19- State the stopping condition before writing the recursive call: when the remainder or one argument becomes 0, the other value is the GCD.
- Give the time complexity of GCD as O(log of the smaller input) and of the Sieve as O(N log log N).
- Handle the overflow edge case in LCM by dividing a by the GCD before multiplying by b.
- On the whiteboard, dry run GCD(12, 18) showing each remainder step, then mark multiples of 2 and 3 as composite to show how the Sieve crosses out non-primes up to 20.
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 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.
- Square Root Bound Reasoning20%
- Dry Run Discipline18%
- Complexity Articulation18%
- Edge Case Handling16%
- Algorithm Choice Judgment14%
- Out Loud Reasoning14%
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 Preparation Sheet 2026: Aptitude, Verbal and Coding Questions - GeeksforGeeksgeeksforgeeks.org
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))? - GeeksforGeeksgeeksforgeeks.org
- Euclidean algorithms (Basic and Extended) - GeeksforGeeksgeeksforgeeks.org
- TCS Ninja Recruitment Process 2026 for Freshers - PrepInstaprepinsta.com
- TCS NQT Coding Questions and Answers 2026 - PrepInstaprepinsta.com