Brands With Continuously Increasing Sales
Reported by candidates from Mastercard's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Mastercard OA question reported in July 2024 looks like a simple trend check, but the whole thing hinges on one structure: the ordered per-brand row sequence you get from a window function. Think LAG over brand_id, ordered by sale_month. You have a brand_sales table, one row per brand per month, and you need brands with at least three consecutive months where sales strictly climb. It's a gaps-and-islands variant in disguise. If you blank on the grouping step, StealthCoder is the hedge that stays invisible while you work the live OA.
The problem
Each row in brand_sales contains one brand's total sales for one calendar month. Return the brands that have at least three recorded months, whose recorded months are immediately consecutive, and whose sales amount increases strictly every month. Return one row per qualifying brand. Tables brand_sales: brand_id PK (Integer), sale_month PK (Date), sales_amount (Decimal) Constraints (brand_id, sale_month) is unique. sales_amount is nonnegative. A brand must have at least three immediately consecutive calendar months. Every sales amount must be strictly greater than the previous month's amount. Sort by brand_id ascending.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is islands. Use LAG to pull the previous month and previous amount per brand. Flag a row as good when the previous month is exactly one calendar month earlier and sales_amount is strictly greater than the previous amount. Any row that breaks the chain starts a new island. Build a running count of breaks per brand with SUM over a window, then group by brand_id and that island number. Keep islands with at least three rows, then SELECT DISTINCT brand_id and order ascending. The common pitfall is comparing only amounts and forgetting the month gap, so a skipped month silently passes. Another is using >= instead of >. Dates need a real month diff, not a day subtraction. Run the logic on a brand with a gap before submitting. If the window logic slips under pressure, StealthCoder can surface a working query in the live OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Brands With Continuously Increasing Sales 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Mastercard's OA.
Mastercard reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Brands With Continuously Increasing Sales FAQ
What's the trick for the Mastercard brand sales question?+
Treat it as gaps and islands. Use LAG on sale_month and sales_amount partitioned by brand_id. Mark rows that continue the streak, meaning exactly one month later and a strictly higher amount. Everything else starts a new island. Count island sizes and keep those with three or more rows.
How do I check months are immediately consecutive?+
Compare the current month to the previous one with a month-level difference, not raw day math. Since sale_month is a date, months have different lengths. Either use a date add of one month on the previous row, or compute year*12+month and check the difference equals 1.
Why not just compare amounts with LAG and LAG twice?+
That works for exactly three months, but the question says at least three. A streak of five also qualifies. Two LAGs would handle the minimal case but get ugly, and you still need the consecutive month check. The islands approach scales to any length streak.
Do I need to worry about duplicates in the output?+
Yes. A brand could have more than one qualifying streak, which would return it twice. The question wants one row per brand, so use SELECT DISTINCT brand_id or group by brand_id, then order by brand_id ascending.
How do I prepare for this in 48 hours?+
Write the LAG plus running SUM islands pattern from memory twice on a toy table. Include a brand with a missing month, a brand with equal sales in two months, and a brand with a long streak. If those three cases pass, you're covered for this style of SQL question.