Reported March 2026
TCSgreedy

Gym Fees Calculator

Reported by candidates from TCS's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live TCS OA. Under 2s to a working solution.
Founder's read

The TCS gym fees question showed up in March 2026, and the first thing you'll notice is that nothing here needs a clever search. The only constraint is months >= 1, so the input can be huge, and any loop that adds one 3-month plan at a time is the wrong move. It's a greedy coin-change setup with a fixed priority order: 12, then 9, then 6, then 3. Take as many of each as you can, then move down. If you've got the OA in a day or two, this is a ten-minute problem once you see it. StealthCoder sits there as a safety net if you blank on the live assessment.

The problem

A gym offers membership plans with the following prices:
3 months: 5000
6 months: 9000
9 months: 12000
12 months: 15000
You are given a requested membership length months. Calculate the total cost using the greedy priority order 12, then 9, then 6, then 3. Use as many plans as possible at each priority before moving to the next smaller plan.
If months is not divisible by 3, return "Error". Otherwise, return the total cost as a string.

Function
calculateGymFees(months: int) → String

Examples
Example 1
months = 18
return = "24000"
Use one 12-month plan and one 6-month plan, for a total cost of 15000 + 9000 = 24000.
Example 2
months = 15
return = "20000"
Use one 12-month plan and one 3-month plan.
Example 3
months = 10
return = "Error"
10 is not divisible by 3, so the requested length cannot be represented by the available plans.

Constraints
months >= 1

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is integer division and remainder, not iteration. Check months % 3 first. If it's nonzero, return "Error". Otherwise, take months / 12 plans at 15000, set months to the remainder, then months / 9 at 12000, then months / 6 at 9000, then months / 3 at 5000. Four steps, constant time. The pitfall is assuming this greedy is globally cheapest. It isn't always, and it doesn't matter, because the statement dictates the priority order. Don't try to be smart with dynamic programming. Another trap is the return type. You must return a string, so convert the total before returning. Use a 64-bit integer if your language needs it, since months has no upper bound. Test 18, 15 and 10 against the examples. If you freeze on the live OA, StealthCoder can give you the four-line solution fast.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Gym Fees Calculator cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass TCS's OA.

TCS reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Gym Fees Calculator FAQ

What's the trick in the TCS Gym Fees Calculator?+

Use division and modulo. Reject months not divisible by 3 with "Error". Then, for plans 12, 9, 6, 3 in order, add (months / size) times the price and set months to months % size. No loops over months are needed.

Do I need dynamic programming here?+

No. The statement fixes a greedy priority order, so you follow it exactly even if a different mix would be cheaper. DP would give different answers on some inputs and fail the tests. Do what the problem says.

How hard is this one really?+

It's easy. The logic is four divide-and-remainder steps plus one divisibility check. Most failures come from forgetting the Error case or returning an integer instead of a string. Read the return type twice.

What edge cases should I test?+

Test months = 3 (returns "5000"), months = 12, months = 10 (Error), and a very large multiple of 3. Also confirm the example 18 gives 24000 and 15 gives 20000. Check the output is a string, not a number.

How do I prepare for this in 48 hours?+

Write the function from scratch twice with different test values. Practice greedy coin-style problems with a fixed denomination order. Spend the rest of the time on input and output formats, since the string return is where people slip.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with TCS.

OA at TCS?
Invisible during screen share
Get it