Most Cost-Effective Machine
Updated
Problem statement
A team has one batch job to run in the cloud and wants the machine type that costs the least for it. The price list comes as CSV text in priceTable. Its first line is the header name,cores,cents_per_hour. Each line after it describes one machine type: its name, its core count, and its price in cents for every hour it runs.
The job needs coreHours core-hours of work. On a machine with c cores the work is shared evenly by all cores, so it runs for coreHours / c hours. The cloud bills every started hour as a full hour. So the machine is billed for ceil(coreHours / c) hours, and its bill is those hours times its hourly price.
Return the name of the machine type with the smallest bill. When several types share the smallest bill, choose the one billed for fewer hours. If that still leaves a tie, choose the one that appears first in the table.
Function
cheapestMachine(priceTable: String[], coreHours: int) → StringExamples
Example 1
priceTable = ["name,cores,cents_per_hour","c8.xl,8,200","c2.sm,2,60","c4.md,4,110"]coreHours = 10return = "c2.sm"c8.xlneeds10 / 8 = 1.25hours, which is billed as 2 hours, so it costs2 × 200 = 400. Without rounding up it would have cost only 250.c2.smneeds exactly10 / 2 = 5hours and costs5 × 60 = 300.c4.mdneeds 2.5 hours, billed as 3, and costs3 × 110 = 330.- 300 is the lowest bill, which makes
"c2.sm"the answer.
Example 2
priceTable = ["name,cores,cents_per_hour","gp-3,3,40","gp-6,6,80","hpc-12,12,170"]coreHours = 24return = "gp-6"- The bills are
8 × 40 = 320forgp-3,4 × 80 = 320forgp-6, and2 × 170 = 340forhpc-12. gp-3andgp-6share the lowest bill.gp-6is billed for 4 hours instead of 8, so it wins even thoughgp-3is listed first.
Example 3
priceTable = ["name,cores,cents_per_hour","n5,5,300","n9,9,300","n3,3,210"]coreHours = 10return = "n5"n5runs10 / 5 = 2hours.n9runs about 1.11 hours, which also bills as 2. Both cost2 × 300 = 600.n3runs about 3.33 hours, billed as 4, for4 × 210 = 840.n5andn9tie on both the bill and the billed hours. The extra cores ofn9do not matter, andn5wins because it comes first in the table.
Constraints
2 <= priceTable.length <= 1001, so the table lists 1 to 1000 machine types.priceTable[0]is exactly"name,cores,cents_per_hour".- Every later line is
name,cores,pricewith no spaces. A name is made of lowercase letters, digits, dots (.) and dashes (-), at most 20 of them and at least one; every name appears on one line only. 1 <= cores <= 512and1 <= price <= 10^6, written as plain decimal numbers with no extra leading 0.1 <= coreHours <= 10^9
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Convert Hexadecimal to Dotted IPv4Microsoft · ONSITE INTERVIEWMicrosoft interview questions
- Caesar Cipher with a Precomputed AlphabetAmazon · ONSITE INTERVIEW
- Filter Contacts by Email DomainStripe · ONSITE INTERVIEWStripe interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Parse and Order Structured LogsCoinbase · ONSITE INTERVIEWCoinbase interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · ONSITE INTERVIEWDuolingo interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)