·Engineering·Medium·20 min

TCS Ninja Number-Theory and Math Coding Drill

20 min · 1 credit · scorecard at the end
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.

  1. 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 = 37
    Example 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.
  2. 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 = 20
    Example 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.
Reference

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.

Square Root Bound Reasoning
Whether the candidate bounds the prime check at the square root of N and can explain that a larger factor must pair with a smaller one already tested, rather than looping to N.
22%
Dry Run Discipline
Whether the candidate traces the program on a concrete sample input, walking the variable values step by step, instead of asserting correctness without verification.
20%
Complexity Articulation
Whether the candidate states the time complexity for each solution, including O of square root of N for the prime check and O of N log log N for the Sieve.
20%
Edge Case Handling
Whether the candidate handles N equal to 0 or 1, negative input, and the LCM and factorial overflow traps without being walked through every one.
20%
Out Loud Reasoning
Whether the candidate narrates their plan and logic out loud while coding so the panellist can follow their reasoning, rather than working in silence.
18%

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

What number-theory programs does the TCS NQT coding section ask?
The recurring TCS NQT number-theory programs are prime check, GCD and LCM, the Sieve of Eratosthenes for primes up to N, factorial and trailing zeros, the Fibonacci series, palindrome numbers, Armstrong numbers, and reversing or summing digits. This drill rehearses the two most common ones, a prime check and GCD with LCM and the Sieve.
Why does a prime check stop at the square root of N?
If N has a factor larger than its square root, that factor pairs with a matching factor smaller than the square root, which you would already have found. So testing divisors only up to the square root of N finds any factor without redundant work, turning an O of N loop into an O of square root of N loop.
What is the time complexity of the Sieve of Eratosthenes?
The Sieve of Eratosthenes runs in O of N log log N. It marks multiples of each prime as composite, and the total marking work sums to N times one half plus one third plus one fifth and so on, which converges to N log log N, far cheaper than running a separate prime check on every number up to N.
How does the Euclidean algorithm find the GCD?
The Euclidean algorithm replaces the pair of numbers with the smaller number and the remainder of the larger divided by the smaller, repeating until the remainder is zero, at which point the last non-zero value is the GCD. It runs in O of log of the smaller input and can be written iteratively with a while loop or recursively.
How do you compute the LCM once you have the GCD?
Once you know the GCD of two numbers a and b, the LCM is a times b divided by the GCD. A common edge case is multiplying a times b before dividing, which can overflow, so divide a by the GCD first and then multiply by b to keep the intermediate value small.
What edge cases matter for these TCS coding problems?
For a prime check, handle N equal to 0 and 1 as not prime and reject negative input. For factorial, watch integer overflow, since 13 factorial overflows a 32-bit int and 21 factorial overflows a 64-bit long. For GCD, handle one input being zero. Interviewers specifically test the N equal to 1 case.
How long is the TCS Ninja coding interview and what is asked?
After clearing the TCS NQT, Ninja candidates face a technical interview where the panellist often asks them to write a small program such as prime, factorial, Fibonacci, or palindrome, explain the logic, dry run it on a sample input, and state the time complexity. This drill mirrors that twenty-minute live coding ask.
Is the TCS Ninja profile still separate from TCS NQT?
No. TCS has merged Ninja into NQT, so Ninja profiles are hired through the TCS NQT only. Your NQT score and coding performance decide whether you are slotted into the Ninja, Digital, or Prime track for the Assistant System Engineer Trainee role.
Why does the interviewer make me dry run my code?
A dry run on a concrete sample input is how the panellist checks that your logic actually works, not just that it compiles. Walking through the variable values line by line catches off-by-one and edge-case bugs, and it shows you can reason about your own code, which is exactly what TCS grades in the live round.
What is the difference between the iterative and recursive GCD?
Both use the same modulo recurrence. The recursive version calls itself with the smaller number and the remainder until the base case where one argument is zero, using call-stack space proportional to the number of steps. The iterative version does the same with a while loop and constant extra space, avoiding any stack-overflow risk on large inputs.
Which languages can I use in the TCS NQT coding round?
The TCS NQT coding section accepts C, C++, Java, Python, and Perl, with two coding questions in a 90-minute window across a Foundation and an Advanced section. For these number-theory problems the logic is the same across languages, so use the one whose syntax you know best and can write without hesitation.

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.