URL Segment Compression Part 3 — Global Token Budget
Reported by candidates from Stripe's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Stripe's November 2023 OA has a Part 3 that punishes anyone who pattern-matches too fast. It's a string problem dressed up as a budgeting problem. You split a path on / and., hand out a global token budget left to right, then compress. The edge case that breaks a naive solution is the merge: the final token must be built from original characters, not already-compressed text. If you blank on the details, StealthCoder is the safety net running invisibly during the live OA. Read this first and you'll know what to build.
The problem
Continue the URL compression sequence. A path has major parts separated by / and minor parts separated by.. Base-compress a word by keeping its first and last character and replacing the middle with the number of omitted characters. Each major may contain at most m output tokens, and the complete path may contain at most t output tokens. Every major must retain at least one token. For this practice version, assign one token to every major first. Give the remaining global token budget to major parts from left to right, never exceeding m or that major's original minor-part count. Within a major with budget q, compress its first q-1 minor parts separately, concatenate all remaining original minor parts, and compress that concatenation as its final token. The merge always uses original characters, not already-compressed text. Practice sequence Part 1: compress every minor part Part 2: cap each major separately Part 3: enforce a global token budget (current) Function compressGlobal(s: String, m: int, t: int) → String Examples Example 1 s = "alpha.beta.gamma/delta.echo.foxtrot/golf.hotel.india" m = 2 t = 5 return = "a3a.b7a/d3a.e9t/g12a" One token is reserved per major. The two remaining tokens go to the first and second majors, so their budgets are [2,2,1]. Example 2 s = "stripe.com/payments/checkout/customer.john.doe" m = 2 t = 4 return = "s7m/p6s/c6t/c13e" There are four majors and four global tokens, so every major receives one token and merges all of its original minor parts. Constraints 1 <= m. Let k be the number of major parts. k <= t <= the sum of all min(m, minorCount) values. The input has no empty major or minor parts. Every minor part has length at least 2. The middle count may contain multiple digits.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two clean passes. First, parse into majors, each with its list of minor parts. Give every major one token, then walk left to right and hand out the leftover budget, capped by min(m, minorCount) minus the one already given. That gives you a budget q per major. Second, for each major, compress the first q-1 minors on their own, then concatenate all the remaining original minors and compress that string as one token. The pitfall is compressing first and merging after. Example 2 shows it: customer.john.doe becomes c13e, because the merged length is 15 and you omit 13 middle characters. Also watch multi-digit counts, and join tokens with. inside a major and / between majors. If the budget logic slips under pressure, StealthCoder can give you a working solution during the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill URL Segment Compression Part 3 — Global Token Budget 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Stripe's OA.
Stripe reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
URL Segment Compression Part 3 — Global Token Budget FAQ
What's the trick in Stripe's URL Segment Compression Part 3?+
Separate budgeting from compression. Compute a token budget per major first, one each, then extra tokens left to right with caps. Only then build output. The last token of each major merges the raw leftover minor parts, never compressed text.
How do I compute the per-major budget?+
Start every major at 1. Remaining equals t minus k. Go left to right and add min(remaining, min(m, minorCount) - 1) to each major, subtracting from remaining. Example 1 gives [2,2,1] with m=2 and t=5.
How is a merged token compressed?+
Concatenate the original characters of all leftover minor parts, then keep the first and last character and put the count of middle characters between them. For customer.john.doe that's 'customerjohndoe', 15 characters, so c13e.
What edge cases should I test before submitting?+
Test a major with q equal to its minor count, so no merging happens beyond the last single part. Test budgets of exactly k. Test middle counts of 10 or more, which produce multi-digit numbers. Also test a single-major path with no slashes.
How do I prepare for this in 48 hours?+
Write a small parser for / and. splitting, then a compress(word) helper, then the budget loop. Run both examples by hand. This is simulation with careful string handling, not hard algorithms, so clean structure and edge-case checks matter most.