Coding problems
Basics and maths
Loops and arithmetic: digits, primes, sequences and patterns.
Learn: Basics and maths: loops, digits and number sizes7 minute read
The first coding questions in a tester interview are usually about numbers: reverse an integer, count its digits, check for a prime, print a star pattern. They are short, so the interviewer watches two things closely. Do you know the few arithmetic tricks these problems use? And do you think about edge cases before you write code? Both matter in testing work too: test data, IDs and amounts are numbers, and their limits are where bugs live.
Taking a number apart with division
Almost every digit problem uses two operations. The remainder after dividing by 10 (written n % 10) is the last digit. Dividing by 10 and dropping the fraction removes the last digit. Repeat until the number is 0 and you visit every digit from right to left. To build a number from digits, go the other way: multiply what you have by 10 and add the next digit.
Take 4096. The last digit is 6, and 4096 becomes 409. Then 9, and it becomes 40. Then 0, then 4, and the number is 0, so the loop stops. Adding the digits gives 19. Building a new number from them in the order you took them (6, then 69, 690, 6904) gives the reverse. This one loop answers "sum of digits", "reverse an integer", "count the digits", "palindrome number" and "Armstrong number". The program below runs it in both languages.
public class Main {
public static void main(String[] args) {
int n = 4096;
int sum = 0;
int reversed = 0;
while (n > 0) {
int digit = n % 10;
sum += digit;
reversed = reversed * 10 + digit;
n /= 10;
}
System.out.println(sum + " " + reversed);
System.out.println(-7 / 2 + " " + -7 % 10);
int big = 2147483647;
System.out.println(big + 1);
System.out.println((long) big + 1);
}
}
19 6904 -3 -7 -2147483648 2147483648
n = 4096
total = 0
reversed_value = 0
while n > 0:
digit = n % 10
total += digit
reversed_value = reversed_value * 10 + digit
n //= 10
print(total, reversed_value)
print(-7 // 2, -7 % 10)
print(2147483647 + 1)
19 6904 -4 3 2147483648
Negative numbers behave differently
Look at the second line of each output. Java's integer division rounds toward zero, so -7 / 2 is -3, and the remainder keeps the sign of the number being divided, so -7 % 10 is -7. Python's floor division rounds down, so -7 // 2 is -4, and its remainder takes the sign of the divisor, so -7 % 10 is 3. A digit loop written for positive numbers can quietly break on negative input. The simple fix in both languages: remember the sign, work with the absolute value, and put the sign back at the end.
Integer sizes and overflow
Java's int holds 32 bits: from -2,147,483,648 to 2,147,483,647. Adding 1 to the largest value wraps around to the smallest one without any error, as the third line shows. This silent wrap-around is called overflow. A long holds 64 bits, up to about 9.2 × 10¹⁸. Python integers have no fixed size, so the same addition just gives 2,147,483,648.
Interviewers test this on purpose. Reversing 1,534,236,469 gives a number that does not fit in 32 bits. The 48th Fibonacci number does not fit either, and the sum of 1 to 100,000 is about five billion. In Java, do risky arithmetic in a long and check the range at the end. In Python the arithmetic is safe, but if the problem says "return 0 when the result does not fit in 32 bits", you still check the range yourself.
Count the work before you write the loop
Interviewers want the cost of your solution, written in a short notation. O(d) (say "order d") means the number of steps grows in step with d: twice the digits, about twice the steps. O(n²) means doubling n makes the work four times bigger. O(1) means the work stays the same however large the input is. "Time" counts steps and "extra space" counts memory beyond the input.
Some basic problems are really about speed. To check whether n is prime you do not need every divisor below n: divisors come in pairs, d and n / d, and the smaller one is at most the square root of n. For two billion that is about 45,000 checks instead of two billion. To list every prime up to n, the Sieve of Eratosthenes crosses out multiples of each prime instead of testing each number on its own.
Each problem on this site is checked against hidden tests, with a time limit. A safe budget: plan for at most about 100 million simple steps in Java and about 10 million in Python for the largest input. A loop inside a loop over 100,000 values is billions of steps (about five billion when it only visits each pair once), which fails in both languages.
How to answer in an interview
- Repeat the problem in your own words and ask about the input range: can it be negative, 0, or larger than 32 bits?
- Say the edge cases out loud before you code (see the list below).
- Explain the idea in one sentence, such as "take digits off with % 10 and / 10", then write the loop.
- Trace one small example by hand, like 4096 above, and state the cost: O(d) for d digits, O(√n) for the prime check.
Common mistakes
- A loop that runs "while n > 0" and so gives the wrong answer for 0 or for negative numbers.
- Arithmetic in a 32-bit
intthat overflows: sums, reversed numbers, Fibonacci numbers, factorials. - Forgetting that 0 and 1 are not prime, or that 0 has one digit.
- Off-by-one errors at rule boundaries: 100, 101, 200 and 201 units for a slab bill; 1900, 2000 and 2024 for leap years.
- Trying every divisor, or adding a number to itself a billion times, when a smarter loop exists.
Recap
Most number questions need only % 10 and / 10, a sign rule for negatives, and a wider type when values grow. Ask about the input range, list the edge cases, and count the steps before you code.
12 questions0/12 solved
| Status | Problem | Difficulty | Asked in |
|---|---|---|---|
| Not started | Count the digits of a number | Easy | Asked in: JPMorgan Chase, TCS |
| Not started | Reverse the digits of an integer | Easy | Asked in: Capgemini, Infosys, Wipro |
| Pro+ | Swap two numbers without a third variable | Easy | Asked in: Accenture, Capgemini, Infosys +1 |
| Pro+ | The first n Fibonacci numbers | Easy | Asked in: Capgemini |
| Pro+ | Check whether a year is a leap year | Easy | Asked in: Infosys |
| Pro+ | Check whether a number is an Armstrong number | Easy | Asked in: Oracle, VMware |
| Pro+ | Add the digits until one digit remains | Easy | Asked in: Zupee |
| Pro+ | Check whether a number is prime | Easy | Asked in: Accenture, Capgemini, TCS |
| Pro+ | Build a star pyramid pattern | Easy | Asked in: Accenture, Capgemini, Cognizant |
| Pro+ | Compute an electricity bill from unit slabs | Easy | Asked in: Wipro |
| Pro+ | List all primes up to n (Sieve of Eratosthenes) | Easy-Medium | Asked in: Accenture, Capgemini, TCS |
| Pro+ | Multiply two numbers without the multiplication operator | Easy-Medium | Asked in: Accenture |
The first 2 questions of each topic are free. Pro+ opens every question.