Reported January 2024
Goldman Sachsmath

Cutting Metal Surplux

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

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

You're looking at a rod-cutting problem from Goldman Sachs reported in January 2024. You have rods of arbitrary lengths, a fixed sale price per unit, and a cost per cut. The task is to pick a target length, cut all rods to that length, count how many uniform pieces you get, subtract the cutting cost, and maximize profit. It's not about cutting for maximum pieces. It's about finding the single target length that makes the math work.

The problem

The owner of a construction company has a surplus of rods of arbitrary lengths. A local contractor offers to buy any of the surplus, as long as all the rods have the same exact integer length, referred to as saleLength. The number of sellable rods can be increased by cutting each rod zero or more times, but each cut has a cost denoted by costPerCut. After all cuts have been made, any leftover rods having a length other than saleLength must be discarded for no profit. The owner's total profit for the sale is calculated as: totalProfit = totalUniFormRods * saleLength * salePrice - totalCuts * costPerCut where totalUniformRods is the number of sellable rods, salePrice is the per unit length price that the contractor agrees to pay, and the totalCuts is the total number of times the rods needed to be cut. Function Description Complete the function maxProfit in the editor. maxProfit has the following parameter(s): Returns int: maximum possible profit

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you're not optimizing cuts per rod. You're optimizing across all rods for a single saleLength. For each possible target length (typically between 1 and the max rod length), calculate: total pieces from all rods, total cuts needed, then profit = pieces * saleLength * salePrice - cuts * costPerCut. The number of pieces from a rod of length L cut to saleLength is floor(L / saleLength). The cuts per rod is pieces - 1 if pieces > 0, else 0. Iterate through all candidate lengths, compute profit for each, return the maximum. This is brute force but fast enough. If you freeze mid-OA, StealthCoder will remind you of the formula and which values to iterate. Common pitfall: forgetting that cutting a rod into N pieces takes N - 1 cuts, not N.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Cutting Metal Surplux 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Goldman Sachs reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Cutting Metal Surplux FAQ

What if some rods are shorter than the target length?+

A rod shorter than saleLength yields zero pieces and zero cuts. It contributes nothing to profit and isn't discarded in the calculation, just ignored. Only rods at least as long as saleLength produce sellable pieces.

How many candidate lengths do I need to test?+

Test every integer from 1 up to the maximum rod length. You're looking for the single length that maximizes the profit formula. Brute force iteration is the right approach here, no need for binary search or fancy optimization.

What's the most common mistake on this problem?+

Miscounting cuts. Remember, cutting a rod into K pieces requires K minus 1 cuts, not K. And if a rod is shorter than saleLength, it produces zero pieces and zero cuts, no error.

Is this a dynamic programming problem?+

No. It's a brute force search over candidate lengths. No overlapping subproblems, no optimal substructure in the DP sense. Calculate profit for each length and pick the best one.

What if all target lengths give negative profit?+

Return the least negative profit, or zero if your company won't cut at all. The problem asks for max profit, so even a loss is the answer if every option loses money. Check if doing nothing (saleLength = infinity) is valid based on the problem statement.

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

OA at Goldman Sachs?
Invisible during screen share
Get it