FastPrepGym Fees Calculator
Problems/Gym Fees Calculator

Gym Fees Calculator

Tata Consultancy Services logoTata Consultancy ServicesOA

Updated Mar 2026

QuestionFunctionExamplesConstraints

Problem statement

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 = 18return = "24000"

Use one 12-month plan and one 6-month plan, for a total cost of 15000 + 9000 = 24000.

Example 2

months = 15return = "20000"

Use one 12-month plan and one 3-month plan.

Example 3

months = 10return = "Error"

10 is not divisible by 3, so the requested length cannot be represented by the available plans.

Constraints

  • months >= 1

More Tata Consultancy Services Online Assessment questions

  • EV Charging Cost OptimizationOA · Seen Sep 2026
  • Maximum Gap After SortingOA · Seen Sep 2026
  • Count Array InversionsOA · Seen Aug 2026
  • Count Smaller Prior ElementsOA · Seen Aug 2026
  • Array Rank TransformOA · Seen Jul 2026
All Tata Consultancy Services interview questionsSee Tata Consultancy Services hiring insights
Saved
class Solution {
    public String calculateGymFees(int months) {
      // write your code here
    }
}
Checking account…