Cutting Metal Surplus
Updated
Problem statement
For this exercise, assume you own rods with integer lengths in lengths. Choose one positive integer saleLength. You may cut each rod into zero or more sellable pieces of exactly that length; unsold leftovers have no value. Each physical cut costs costPerCut.
Each sold piece earns saleLength * salePrice. An uncut rod of exactly the sale length can be sold without a cut. You may leave any rod unused, including when cutting it would lose money.
Choose the sale length and what to sell from each rod to get the largest profit. Return the total money earned minus the cutting costs. Do not change the input array.
Function
maxProfit(lengths: int[], costPerCut: int, salePrice: int) → intExamples
Example 1
lengths = [8,12]costPerCut = 1salePrice = 3return = 57Choose sale length 4: sell five pieces, earning 60 with three cuts costing 3.
Example 2
lengths = [5,5]costPerCut = 100salePrice = 1return = 10Choose sale length 5 and sell both rods uncut, avoiding every cut cost.
Example 3
lengths = [4,7]costPerCut = 2salePrice = 1return = 7Sell the length 7 rod uncut and leave the other rod unused.
Constraints
1 <= lengths.length <= 50.1 <= lengths[i] <= 10000.1 <= costPerCut, salePrice <= 100.- The maximum profit fits a signed 32-bit integer.
More Roblox Online Assessment questions
- Centered Block Placement QueriesOA · Seen Oct 2026
- Robot Navigation Around LasersOA · Seen Oct 2026
- Factory Batch Profit (Game Adaptation)OA · Seen Sep 2026
- Nested Recipe Blocks (Game Adaptation)OA · Seen Sep 2026
- Vehicle Builds for Required Terrain (Game Adaptation)OA · Seen Sep 2026
- Track Received Byte RangesOA · Seen Sep 2026
- Break a PalindromeOA · Seen Jun 2026
- Count Pairs Differing in One DigitOA · Seen Oct 2025
- Find Zero Cells in a 2×2 GridOA · Seen Oct 2025
- Cat Route Budget Planning (Game Adaptation)OA · Seen Aug 2025
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Count Subarrays with a Perfect-Square ProductMicrosoft · OAMicrosoft interview questions
- Repeated Mirror XORMathWorks · OAMathWorks interview questions
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Power-of-Two Removal OperationsIBM · OAIBM interview questions
- Macaron Order PriceOpenAI · OAOpenAI interview questions
- Daily Settlement for Long and Short PositionsBelvedere Trading · OA
Interview reports that mention this question
Roblox interview experiences
- Roblox New Grad Engineer Rounds: 13 Experiences (Part 6 of 7)
- Roblox New Grad HackerRank OA: 18 Experiences (Part 5 of 7)
- Roblox New Grad CodeSignal Finals: 23 Experiences (Part 4 of 7)
- Roblox New Grad Games and BQ: 25 Experiences (Part 3 of 7)
- Roblox New Grad Coding Rounds: 23 Experiences (Part 2 of 7)