TCS CodeVita Questions 2026: 7 Official Samples Solved
All 7 sample problems on the official CodeVita portal: tested code for 4, the key idea for the 3 geometry ones, and the Java trap in Codu and Sum Love.
on this page§ 10

Most "CodeVita previous year questions" lists online are generic coding problems with a CodeVita label stuck on them. The seven problems on TCS's own portal are the real style guide: long story-style statements, an exact output format, and one detail that decides whether you pass the hidden test cases. That detail is what this page is about. For dates, eligibility and prizes, see the TCS CodeVita Season 14 guide.
The Official Sample Set at a Glance
| Problem | Official label | What it really tests | The detail that decides it |
|---|---|---|---|
| Square Free Numbers | Simple | Prime factors | Only the count of distinct primes matters |
| Codu and Sum Love | Simple | Reading Java exactly | (long) Math.pow(2, x) stops growing at x = 63 |
| Obstacle Game | Simple | Grid walk, 8 directions | Diagonal moves count, Music is not a hurdle |
| Sorting Boxes | Complex | Permutation cycles | Borrowing the lightest box can be cheaper |
| On A Cube | Complex | 3D geometry | Same-face moves are 60-degree arcs, not straight lines |
| Water Cistern | Complex | Unrolling a cylinder | The path over the lid can beat the path around the side |
| Sport Stadium | Complex | Search with constraints | A whole wet block can stay empty if it is under 15 seats |
Every problem on the portal has a 1-second time limit. The code below is Python 3, because Python is one of the four languages allowed in Season 14 and it is the easiest to read, plus one C++ version where Python is too slow. Each block was run against both official examples for its problem on 15 October 2026 and produced the expected output.
Season 14 Rules That Change How You Practise
These come from the official portal and its FAQ, checked on 15 October 2026:
- 3 hours, four languages. Participants get 3 hours, and only C, C++, Java and Python are allowed this season.
- Rounds thin out fast. Only the top coders in each round move on, and the portal says Rounds 2 and 3 may involve technologies beyond traditional programming, with details after Round 1.
- Order is free, the clock is not. You can attempt questions in any order, but if you move to question 2 without submitting question 1, the timer for question 1 keeps running.
- Two kinds of test cases. Results show up under separate tabs for public and private test cases. The examples in a statement are only the public part.
- Save your code locally. If you close the browser, you lose your work. The session also expires after 15 minutes of inactivity.
- Declare borrowed code. You may refer to outside material, but you must give the precise URL of any code you used through the attribution option before submitting, or the code may be treated as plagiarised.
The practice lesson from the first and fourth rules: open a problem only when you mean to finish it, and treat the public examples as a minimum, not as proof.
Problem 1: Square Free Numbers (Simple)
The task, in short: given N with no prime factor larger than 19 (N below 10^9), count the divisors of N that are square-free, not counting 1. For N = 20 the answer is 3 (2, 5 and 10). For N = 72 it is 3 (2, 3 and 6).
The idea: a square-free divisor uses each prime at most once, so it is just a non-empty choice from the distinct primes of N. If N has k distinct primes, the answer is 2^k - 1. Exponents do not matter: 72 = 2^3 x 3^2 has two distinct primes, so 2^2 - 1 = 3.
n = int(input())
k = 0
for p in (2, 3, 5, 7, 11, 13, 17, 19):
if n % p == 0:
k += 1
while n % p == 0:
n //= p
print(2 ** k - 1)
There is no need to list divisors at all. That is the point of a Simple problem in CodeVita: the statement walks you through listing divisors, and the fast answer ignores that path.
Problem 2: Codu and Sum Love (Simple, With a Trap)
The task, in short: the statement gives a few lines of Java and asks you to reproduce its output. For each of N numbers x, the code computes (long) Math.pow(1 << 1, x), which is 2 to the power x, keeps the last two digits of the result as text, adds them up, and prints the sum modulo 100. N goes up to 10^7 and x up to 10^18.
The trap: if you compute the true value of 2^x, you get the wrong answer for large x. Java's Math.pow returns a double, and the cast to long cannot go above the largest long value. The Java Language Specification (section 5.1.3) says a floating-point value too large for a long converts to the largest representable long, which is 9,223,372,036,854,775,807. So for every x from 63 upward, the Java code sees a number ending in 07 and adds 7, not the last two digits of the real 2^x. For x up to 62 the value is exact, because Math.pow returns the exact result when it fits in a double and powers of two do.
Check it on the official example: inputs 8, 6, 7 and 4 give 256, 64, 128 and 16, whose last two digits sum to 56 + 64 + 28 + 16 = 164, and 164 modulo 100 is 64. That matches the official output.
import sys
data = sys.stdin.buffer.read().split()
n = int(data[0])
total = 0
for tok in data[1:1 + n]:
x = int(tok)
if x >= 63:
total += 7 # the Java cast saturates at 9223372036854775807
else:
total += (1 << x) % 100
print(total % 100)
Speed is the second catch. On our machine, this Python version took about 3.4 seconds on a test file with the maximum 10 million numbers, even reading the whole input at once, which is far over the 1-second limit. The same logic in C++ took about 0.8 seconds on the same file and printed the same answer:
#include <cstdio>
int main() {
long long n, x, total = 0;
if (scanf("%lld", &n) != 1) return 0;
for (long long i = 0; i < n; i++) {
if (scanf("%lld", &x) != 1) break;
if (x >= 63) total += 7; // Java's cast saturates here
else total += (long long)((1ULL << x) % 100);
}
printf("%lld\n", total % 100);
return 0;
}
The wider lesson for CodeVita: when a problem says "perform logic equivalent of the code", it is testing whether you notice what the language does at the edges, not whether you can do arithmetic.
Problem 3: Obstacle Game (Simple)
The task, in short: an N x N grid (N from 2 to 20) has a start A at the top-left, a destination D, a single route of R cells that can run straight or diagonally, four hurdle types (S stone, L wall, W water, T thorn) and M for music, which is not a hurdle. Walk the route and, at every R step, print the hurdles in the 8 surrounding cells in ascending order, or NO HURDLES. Print DESTINATION on reaching D.
The idea: a plain walk. From the current cell, look at all 8 neighbours for the next unvisited R (or D), and print the sorted hurdle letters around each R. Sorting the letters alphabetically gives the official order: L, S, T, W.
n = int(input())
g = [input().split() for _ in range(n)]
HURDLES = set("SLWT")
DIRS = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
def inside(r, c):
return 0 <= r < n and 0 <= c < n
r, c = 0, 0
seen = {(0, 0)}
while True:
nxt = None
for dr, dc in DIRS:
rr, cc = r + dr, c + dc
if inside(rr, cc) and (rr, cc) not in seen and g[rr][cc] in "RD":
if g[rr][cc] == "D":
nxt = (rr, cc)
break
if nxt is None:
nxt = (rr, cc)
r, c = nxt
seen.add(nxt)
if g[r][c] == "D":
print("DESTINATION")
break
around = sorted(g[r + dr][c + dc] for dr, dc in DIRS
if inside(r + dr, c + dc) and g[r + dr][c + dc] in HURDLES)
print(" ".join(around) if around else "NO HURDLES")
One judgement call: the statement promises a single continuous route but does not say what to do if the route passes next to D before its end. This code steps onto D as soon as it is adjacent, which matches both official examples. If you hit this in a real round, re-read the statement for a tie-break rule before submitting.
Problem 4: Sorting Boxes (Complex)
The task, in short: N boxes (up to 50, weights up to 1,000, all different) must end up in increasing order, except that the heaviest box must sit at position k. You may swap any two boxes, and a swap costs the product of their weights. Find the minimum total effort. The official examples give 3,600 and 7,600.
The idea: compare the current order with the target order. The misplaced boxes split into cycles: box A sits where B belongs, B sits where C belongs, and so on back to A. Each cycle is fixed independently, in one of two ways:
- Use the cycle's own lightest box m. Swap m with each other box in the cycle in turn. Cost: m x (sum of the cycle - m).
- Borrow the lightest box overall, g. Swap g into the cycle, use it to place every other box, then swap it back. Cost: g x (sum of the cycle + m).
Take the cheaper one for each cycle and add them up. In the first official example (weights 20 50 30 80 70, k = 2) only 50 and 80 are misplaced. Swapping them directly costs 4,000; borrowing the 20 costs 20 x (130 + 50) = 3,600, which is the official answer. A side note for careful readers: the portal's worked explanation prints one intermediate row as 20 80 30 80 70, which is a typo for 20 80 30 50 70.
def min_effort(k, w):
heavy = max(w)
rest = sorted(x for x in w if x != heavy)
target = rest[:k - 1] + [heavy] + rest[k - 1:]
where = {v: i for i, v in enumerate(target)}
g = min(w)
seen = [False] * len(w)
total = 0
for i in range(len(w)):
if seen[i] or where[w[i]] == i:
seen[i] = True
continue
cycle, j = [], i
while not seen[j]:
seen[j] = True
cycle.append(w[j])
j = where[w[j]]
m, s = min(cycle), sum(cycle)
total += min(m * (s - m), g * (s + m))
return total
n, k = map(int, input().split())
print(min_effort(k, list(map(int, input().split()))))
How we checked it beyond the two examples: we compared this function with an exact search (Dijkstra's shortest path over every arrangement of the boxes) on 1,500 random inputs with 2 to 7 boxes. The answers matched on all 1,500. That is strong evidence, not a proof, for larger N, but it is the kind of check worth doing in a round when a greedy idea feels right.
Problems 5 to 7: The Three Geometry Problems
These are the Complex problems where a full solution runs long. Here is the idea each one turns on, so you can write the rest.
On A Cube
A beetle crawls over the surface of a 10 x 10 x 10 cube, visiting points in order, never using the bottom face. Two rules: on the same face it moves along a circular arc that subtends 60 degrees at the circle's centre; between faces it takes the shortest path over the surface. Each leg is rounded to two decimals before adding.
- Same face: a 60-degree arc means the centre and the two points form an equilateral triangle, so the radius equals the straight-line distance c, and the arc length is pi x c / 3. For c = 1 that is 1.05, as in the official example.
- Different faces: unfold the faces into a flat sheet and take the straight line. For neighbouring faces, try the direct unfolding and the routes through one more face, never through the bottom, and keep the shortest. In the official example, (2, 1, 10) on the top to (0, 1, 9) on the side x = 0 is 2 across the top plus 1 down the side: 3.
- Rounding: round each leg, then add. Adding first and rounding once can change the last digit.
Water Cistern
A bug on the curved side of a cylinder (radius r, height h, starting s below the top) must reach a point either on the side or on the top lid. The bottom is not accessible.
- Side to side: unroll the curved surface into a rectangle. The distance is the square root of (r x angle)^2 + (height difference)^2, using the smaller angle in radians. The first official example (same height, opposite side) is half the circumference: 314.
- Side to lid: climb to some point on the rim, then go straight across the lid. The best rim point is not always the one directly above; you minimise over it. The second official example has it directly above: 200 + 100 + 50 = 350.
- The case most attempts miss: for two points on the side, going over the lid can be shorter than going around. With r = 99, both points 41 below the top and on opposite sides (allowed by the constraints, which put s above 40 and r below 100), going around costs about 311, while going straight up, across the lid and down costs 41 + 198 + 41 = 280. A solution that only unrolls the side fails that test.
Sport Stadium
A contingent of N fans must sit in one row of S seats split into M blocks that alternate dry and wet, starting dry. Only k fans accept wet seats. Minimise the distance between the first and last fan, with no run of 15 or more empty seats between them; print 0 if it is impossible.
- Read the second official example closely: the best seating leaves the whole wet block 51-60 empty, because 10 empty seats is under the limit, and puts only 5 fans in the wet block 21-30.
- Search space: S, N and k are all below 1,000 and M is below 30, so trying every first seat and finding the shortest window that can hold everyone under the three rules (dry seats, at most k wet seats, no 15-seat gap) is within reach in C++. Write the feasibility check for a window first and test it on both examples before optimising.
How to Use These Seven Problems
- Solve the three Simple ones under a timer, 20 minutes each, in the language you will use on the day. They train the CodeVita habit of finding the one detail the statement hides.
- Attempt Sorting Boxes before reading the idea above. If your first answer to example 1 is 4,000, you have found what the problem is testing.
- For each geometry problem, write down the edge case before any code. All three punish code that handles only the examples.
- Run MockVita when the portal offers it. The portal describes MockVitas as rounds just like the actual contest, so they are the closest thing to a real paper.
For the aptitude-plus-coding route into TCS, which has a fixed format, pair this with the TCS NQT coding section guide, the TCS coding questions bank and TCS placement papers.
WHAT PAPERSADDA THINKS
The official sample set is short, but it shows exactly what CodeVita rewards: reading the statement more carefully than everyone else. Two of the three Simple problems are lost by people who code what the statement narrates instead of what it means, and two of the Complex ones hide a cheaper path that the examples barely hint at. If you have a week before your round, spend it on seven problems done properly, with your own edge cases, rather than on a hundred generic "CodeVita questions" copied from sites that do not run their own code.
Frequently Asked Questions
Where can I find official TCS CodeVita questions?
The official portal at codevita.tcsapps.com has a Sample Questions section with seven problems: On A Cube, Sorting Boxes, Sport Stadium, Water Cistern, Square Free Numbers, Codu and Sum Love, and Obstacle Game. Each has a problem statement, constraints, a difficulty label, a 1-second time limit and two worked examples. TCS does not publish full past-round papers, so anything sold as a leaked CodeVita paper is not official.
Which languages are allowed in TCS CodeVita Season 14?
The official portal says only four languages are allowed this season: C, C++, Java and Python. It also says participants get 3 hours, and that only the top coders in each round move on.
How hard are TCS CodeVita questions?
The official sample set labels three problems Simple and four Complex. The Simple ones test one idea each (a divisor-counting formula, a Java overflow rule, a grid walk). The Complex ones are geometry or optimisation problems where the obvious approach misses a case, such as Sorting Boxes, where routing swaps through the lightest box beats direct swaps.
Is Python fast enough for CodeVita?
Python is one of the four allowed languages and is fine for most sample problems, but read the constraints first. Codu and Sum Love allows up to 10 million numbers in one input, so read the whole input at once instead of calling input() in a loop. If a problem's constraints are large and the time limit is 1 second, C++ is the safer choice.
Can I use code from the internet in CodeVita?
The official FAQ says you may refer to other material, but you must declare it: before submitting there is an option to give attribution, and you must give the precise URL of any code used. If a precise URL is not provided, the code might be considered plagiarised.
Sources and review notesreviewed 15 Oct 2026
Official notices, candidate reports, offer documents, and editorial practice questions carry different confidence levels. The visible source list lets you inspect the evidence instead of relying on a blanket verification badge.
topic cluster
More resources in Company Placement Papers
Use the category hub to browse similar questions, exam patterns, salary guides, and preparation resources related to this topic.
Start with the pillar guide: TCS NQT 2026: iON Test 24 Nov, Pattern, Cutoffs & Salary - the complete, source-anchored reference for this cluster.
company hub
Explore all Tata Consultancy Services resources
Open the Tata Consultancy Services hub to jump between placement papers, interview questions, salary guides, and related pages in one place.
paid contributor programme
Sat Tata Consultancy Services this year? Share your story, earn ₹500.
First-person experience reports help future candidates prep smarter. We pay verified contributors ₹500 via UPI per accepted story with byline.
Submit your story →ready to practice?
Take a free timed mock test
Put what you learned into practice. Our mock tests match the 2026 pattern with timer, navigator, reveal, and score breakdown. No signup.