Coding Questions in Service-Company Tests (2026): The Repeated Problems and Why People Fail Them
Updated August 2026
This page is about the written coding round — the timed online test — and not about the technical interview. The distinction matters because they fail people differently. Our DSA set covers what an interviewer asks you to explain out loud, including complexity, hash maps, palindromes and two-sum. This page covers what you have to type correctly, alone, against a clock.
The most useful thing to know is that the difficulty tier is lower than students fear and the failure rate is higher than that tier suggests. These tests typically set two problems at easy to lower-medium level — string manipulation, digit arithmetic, array scans, occasionally a pattern to print. Almost nobody fails them because the algorithm was too hard. They fail because the input arrived in a format they had not practised reading, because an edge case broke a nearly-correct solution, or because forty minutes went into the first problem and the second was never attempted.
So the preparation that pays is not harder problems. It is knowing the recurring shapes cold, being fluent in one language's string and array handling, practising input parsing until it is automatic, and having a deliberate strategy for partial scoring. Each entry below gives the problem, the pattern behind it, and the edge case that actually catches people.
Frequently asked questions
What difficulty are these tests actually set at?
Easy to lower-medium, in the vocabulary of practice sites — typically two problems in roughly an hour, sometimes alongside aptitude and technical sections. Expect string handling, digit arithmetic, single-pass array work and simple matrix operations. Dynamic programming, graphs and advanced data structures are rare in mainstream service-company fresher tests, though they appear in the higher tiers some companies run for premium packages. The practical implication: grinding hard problems is a poor use of your last month, while being unable to reverse digits without hesitating is a genuine risk.
Why do candidates fail if the problems are easy?
Three reasons, in order of how often they occur. Input handling — the test gives you numbers on one line, or a count followed by that many values, and code that works in your editor fails against the judge because you read the input differently. Edge cases — empty input, a single element, zero, negatives, all-identical values — which break a solution that is otherwise right. And time management, where one problem consumes most of the window and the second is never opened. Notice that none of these is the algorithm, which is where most people spend their preparation.
How does partial scoring change my strategy?
Most of these platforms score per test case, so a working brute-force solution that passes the smaller cases scores considerably more than an elegant one that does not compile. That leads to a strategy worth committing to before you sit down: write the straightforward correct version first, submit it, and only then optimise if time remains. The common and costly mistake is attempting the clever solution first, running out of time, and submitting nothing. Read the constraints too — if the input size is small, brute force is not merely acceptable, it is the intended answer.
Which language should I use?
The one whose string and array library you know without looking up, which for most Indian freshers is Java, Python or C++. Python is fastest to write and forgiving with big integers; Java is the most commonly expected in service-company interviews afterwards; C++ is fine if it is genuinely your language. What matters far more than the choice is fluency with the basics under time pressure — splitting a string, converting types, sorting with a comparator, using a frequency map. Do not switch languages in your final month, and do not pick one because a senior said it was faster.
Print a pyramid or triangle pattern of stars or numbers.
Pattern printing barely exists on international practice sites and appears constantly in Indian service-company tests, so practise it specifically. The shape is always the same: an outer loop for rows, an inner loop for leading spaces, another for the characters themselves. Work out the two relationships on paper first — how many spaces and how many symbols each row needs as a function of the row number — and the code writes itself. The usual errors are an off-by-one in the space count and printing a newline in the wrong place.
Check whether a number is prime.
Loop from 2 up to the square root of n and test divisibility; if nothing divides it, it is prime. Two edge cases decide whether you pass: numbers less than 2 are not prime, and 2 itself is prime while being the only even one. The square-root bound is the detail interviewers and test cases both look for, since checking up to n is needlessly slow and will time out on larger inputs. If asked for all primes up to n, the sieve is the expected answer.
Reverse the digits of a number, and sum its digits.
Both use the same loop and are worth practising as a pair, because half the number problems in these tests are variations on it: take the last digit with n % 10, remove it with n / 10 using integer division, and repeat until the number is zero. For reversal you build the result as result = result * 10 + digit. Edge cases that catch people: negative numbers, trailing zeros that vanish on reversal, and integer overflow if you reverse a large number in a fixed-width type. This single loop also gives you digit counting, palindromic numbers, Armstrong numbers and digital roots.
Check for an Armstrong number, and for a perfect number.
Both are direct applications of the digit loop and both appear regularly. An Armstrong number equals the sum of its digits each raised to the power of the digit count — so 153 gives 1 + 125 + 27. Count the digits first, then apply the power. A perfect number equals the sum of its proper divisors, as with 6 giving 1 + 2 + 3, so loop divisors up to n/2 or use the square-root trick and add both members of each pair. These are pure definition-recall problems: the whole difficulty is remembering what the term means.
Find the GCD and LCM of two numbers.
Use the Euclidean algorithm for GCD: repeatedly replace the pair with the smaller number and the remainder of the division, until the remainder is zero. It is three lines and vastly better than looping through candidate divisors. Then LCM follows from the identity that a times b equals GCD times LCM, so compute LCM as a / gcd * b — dividing before multiplying to reduce overflow risk. Edge case: handle a zero input explicitly rather than letting the loop misbehave.
Count vowels, consonants, digits and spaces in a string.
A single pass with a set of vowels for membership testing. The parts that decide correctness are the ones people skip: normalise case before comparing rather than listing ten characters, and be explicit about what happens to punctuation and to characters that are neither letters nor digits. If the problem statement is ambiguous about non-alphabetic characters, choose a reasonable interpretation and be consistent. Reading the whole line including spaces, rather than a single token, is a common input-handling slip here.
Find the frequency of each character in a string.
A frequency map, and this is the single most reusable pattern in the whole round — the same map solves anagram checking, first non-repeating character, duplicate detection and most-frequent-element questions. Build a hash map from character to count in one pass, then read from it. For strings restricted to lowercase letters, an array of 26 counters is a neat alternative worth mentioning. Practise this until it is automatic, because a large share of the string problems in these tests reduce to it.
Check whether two strings are anagrams.
Two approaches and both are accepted. Sort both strings and compare, which is short and obvious at O(n log n). Or count characters into a frequency map and compare the maps, which is linear and the better answer if asked about efficiency. Decide upfront how you treat case and spaces, because "Listen" and "Silent" are anagrams only if you normalise first, and test cases frequently include that variation.
Find the first non-repeating character in a string.
Two passes over the string using a frequency map: build the counts, then scan the original string in order and return the first character whose count is one. The reason for the second pass over the original rather than the map is order — a map does not reliably preserve the sequence you need. The edge case is a string where every character repeats, where you must return whatever the problem specifies rather than crashing on an empty result.
Remove duplicates from an array, and count occurrences of each element.
Both are the frequency map again, or a set if you only need uniqueness. If the array is sorted, or you are allowed to sort it, a two-pointer pass removes duplicates in place without extra memory, which is the answer to give if asked to avoid additional space. State whether your solution preserves the original order, because the problem often requires it and a set-based approach may not.
Find the missing number in an array of 1 to n.
Compute the expected sum with n * (n + 1) / 2 and subtract the actual sum — one pass, no extra memory, and the answer they are looking for. The XOR approach is equally valid and avoids overflow on large n, which is worth naming as an alternative. Read the problem carefully first, though: whether the range starts at 0 or 1, and whether exactly one number is missing, changes the arithmetic entirely and is where most wrong answers originate.
Rotate an array by k positions.
The neat solution is three reversals: reverse the whole array, then reverse the first k elements, then reverse the rest. That rotates in place with no extra array. Two details decide correctness: take k modulo the length, since k can exceed the array size, and be certain which direction the problem means, because left and right rotation are different answers and the statement is often terse. The simpler approach of copying into a new array is perfectly acceptable if space is not constrained.
Find the maximum sum of a contiguous subarray.
This is the hardest thing that commonly appears, and Kadane's algorithm is the expected answer: walk the array keeping a running sum, reset it to the current element whenever it would go negative, and track the best value seen. One pass, constant space. The edge case that catches almost everyone is an array of all negative numbers, where the answer should be the least negative element rather than zero — initialise your best value to the first element rather than to zero and it works.
Transpose a matrix and sum its diagonals.
Matrix questions in these tests stay basic. Transposing means swapping the element at row i, column j with the one at row j, column i, and iterating only over the upper triangle so you do not swap everything back. For diagonals, the primary diagonal is where the row index equals the column index and the secondary is where they sum to size minus one — with the detail that on an odd-sized matrix the centre element belongs to both, which the problem may or may not want counted once. Reading a matrix from the input format correctly is more often the failure point than the logic.
What are pseudo-code questions, and how do I prepare for them?
Some recruiters, Capgemini most notably, use a pseudo-code section instead of or alongside writing real code: multiple-choice questions showing a fragment in a language-neutral syntax and asking what it outputs. There is no compiler and no test cases, so the skill being measured is careful mental tracing rather than construction. Prepare by working through loop and array fragments by hand on paper, tracking variable values line by line. The frequent errors are miscounting loop iterations, missing that an index starts at zero, and rushing a nested loop — all fixed by writing the values down rather than tracking them in your head.
How should I spend my last two weeks before a service-company test?
Not on harder problems. Spend it on the recurring shapes here until they are automatic — the digit loop, the frequency map, a single-pass array scan and pattern printing — and on the two things that actually fail people. Practise reading input in your chosen language in every format these tests use, including a count line followed by values, and make a habit of testing every solution against empty input, a single element, zeros and negatives before submitting. Then do two full timed mock rounds to build the instinct of leaving a problem and returning to it rather than sinking the whole window into one.
Don't just read Service-company coding test questions — get asked them
Phiny's AI interviews you on exactly these topics, follows up on weak answers, and tells you what a stronger answer looks like. Text interviews are free and unlimited.
Start a free AI mock interviewHow to prepare
- Practise input parsing, not just algorithms. Code that works in your editor and fails against the judge is almost always reading the input differently, and this single gap accounts for more lost marks in these rounds than algorithmic difficulty does.
- Write the brute force first and submit it. Per-test-case scoring means a working simple solution beats an elegant one that never compiled — and once something is submitted, optimising is upside rather than risk.
- Test four edge cases before every submission: empty, one element, zeros, negatives. That habit takes twenty seconds and catches the majority of near-miss failures.
- Time-box each problem before you start. If a problem is not working at the halfway mark, leave it and open the other one — an unattempted second problem is the most common avoidable loss in the whole round.
- Pair this with our DSA set, which covers what interviewers ask you to explain aloud — complexity, hash maps, linked lists — and with the aptitude set, since most of these tests bundle all three sections into one sitting.
Where these questions get asked
- TCS NQT guide and Infosys hiring guide — the two biggest exams these questions appear in.
- All company placement guides — pattern, syllabus and rounds for every mass recruiter.