EV Charging Cost Optimization
Reported by candidates from TCS's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The TCS EV Charging Cost Optimization question, reported in September 2026, hides a simple idea inside a long story about fleets and slots. Total demand is the sum of vehicles. Each slot gives up to maxCapacity units at its own per-unit cost, and anything left over costs penalty per unit. It's a greedy problem wearing a logistics costume. If you see it in 48 hours, you can solve it in about ten lines. And if the OA clock gets loud and your head goes blank, StealthCoder is the invisible safety net running on your screen.
The problem
A fleet has charging requirements given by vehicles. Only the total required energy matters; energy from any charging slot may be assigned to any vehicle. Each entry in costs represents one charging slot. A slot can provide at most maxCapacity units, and every unit taken from that slot costs the corresponding value in costs. Each slot may be used partially or skipped. After using the slots, every unfulfilled unit costs penalty. Return the minimum total cost needed to cover or penalize the entire demand. Implement minimumChargingCost with integer-array parameters vehicles and costs, integer parameters maxCapacity and penalty, and return the minimum total cost as a long. Function minimumChargingCost(vehicles: int[], costs: int[], maxCapacity: int, penalty: int) → long Examples Example 1 vehicles = [5,7,3] costs = [10,4,8,20] maxCapacity = 6 penalty = 9 return = 99 The total demand is 15. Use 6 units from the cost-4 slot and 6 units from the cost-8 slot, then pay the penalty for the remaining 3 units: 24 + 48 + 27 = 99. Example 2 vehicles = [4,4] costs = [3,5] maxCapacity = 5 penalty = 10 return = 30 Both slots are cheaper than the penalty. Buy 5 units at cost 3 and the remaining 3 units at cost 5. Example 3 vehicles = [10] costs = [12,15] maxCapacity = 4 penalty = 7 return = 70 Every charging slot is more expensive per unit than the penalty, so skip both slots and pay 10 * 7. Constraints 1 <= vehicles.length, costs.length <= 200000 0 <= vehicles[i] <= 10^9 1 <= costs[i], maxCapacity, penalty <= 10^9 The answer fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Sum vehicles into a long. Sort costs ascending. Walk through the slots in order. For each slot, stop if its cost is greater than or equal to penalty, since a penalty unit is no worse. Otherwise take min(maxCapacity, remaining) units, add units * cost to the total, and subtract from remaining. After the loop, add remaining * penalty. That's the whole trick: cheapest slots first, only while they beat the penalty. Example 3 shows the skip case, where both slots cost more than 7, so the answer is 10 * 7 = 70. The pitfall is overflow. Demand can reach 200000 * 10^9, so use long for everything, including the multiplication. Another trap is forgetting that slots can be skipped or partially used. Sorting gives O(n log n), which fits the 200000 limit. If you blank during the live OA, StealthCoder can hand you this greedy loop in real time.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill EV Charging Cost Optimization 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass TCS's OA.
TCS reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
EV Charging Cost Optimization FAQ
What's the trick in the TCS EV Charging Cost Optimization problem?+
Greedy. Sum all vehicle demand, sort slot costs ascending, and fill from the cheapest slot while its cost is below the penalty. Each slot gives at most maxCapacity units. Whatever demand remains gets multiplied by penalty. Nothing else is needed, no DP or graph work.
Why does Example 1 return 99?+
Demand is 5+7+3 = 15. Sorted costs are 4, 8, 10, 20. Take 6 units at 4 (24) and 6 units at 8 (48). The cost-10 slot is more than the penalty of 9, so stop. The remaining 3 units cost 27. Total is 24+48+27 = 99.
What overflow mistakes should I watch for?+
Demand can hit about 2*10^14 and per-unit costs reach 10^9, so products exceed 32-bit by a lot. Use a long for total demand, remaining units, and the running cost. Cast before multiplying, not after, or the intermediate result will overflow silently.
Do I need to compare cost to penalty for every slot?+
Yes. A slot with cost greater than or equal to penalty never helps, because paying the penalty is at least as cheap. Since costs are sorted, you can break at the first such slot. Example 3 is exactly this case, where you skip everything and pay 10 * 7.
How do I prepare for this in 48 hours?+
Write the greedy solution once from scratch with long arithmetic and test the three given examples. Then try edge cases: all vehicles zero, one slot, maxCapacity larger than demand, and penalty lower than every cost. That covers nearly everything this problem can throw at you.