Maximum Profit from an Increasing Price Triplet
Reported by candidates from Odoo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Odoo OA reported in January 2023 hands you two arrays, price and profit, and asks for three days with strictly rising prices and the biggest profit sum. Example 1 returns 15 from indices (3, 4, 5) one-based, and example 2 returns -1 because nothing rises three times. It looks like a longest increasing subsequence cousin, but you only need exactly three picks. That makes it far friendlier than it first appears. If you blank on the setup, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net.
The problem
An analyst observes a stock over n days. The stock price on day i is price[i], and the profit associated with that day is profit[i]. Choose three indices i < j < k such that price[i] < price[j] < price[k]. Return the maximum possible value of profit[i] + profit[j] + profit[k]. If no valid triplet exists, return -1. Function getMaximumProfit(price: int[], profit: int[]) → int Examples Example 1 price = [1, 5, 3, 4, 6] profit = [2, 3, 4, 5, 6] return = 15 The optimal triplet is (3, 4, 5) in one-based indexing. Its prices satisfy 3 < 4 < 6, and its total profit is 4 + 5 + 6 = 15. Example 2 price = [3, 2, 1] profit = [10, 20, 30] return = -1 No three indices have strictly increasing prices.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to fix the middle index j. For each j, find the best profit[i] where i < j and price[i] < price[j], and the best profit[k] where k > j and price[k] > price[j]. If both exist, candidate = left + profit[j] + right. Take the max across all j, else -1. The O(n^2) version does two inner scans per j, and it's fine for modest n. If n is large, use a Fenwick tree or segment tree over compressed prices for prefix max queries, or a sorted structure, to get O(n log n). The common pitfall is using non-strict comparisons, since equal prices don't count. Another is greedily picking the largest profits without checking order. Also remember to return -1, not 0, when no triplet exists. If the live OA freezes you on the optimized version, StealthCoder is the hedge that gets you unstuck.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum Profit from an Increasing Price Triplet 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Odoo's OA.
Odoo reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Profit from an Increasing Price Triplet FAQ
What's the trick to the Odoo maximum profit triplet problem?+
Fix the middle day j. Then find the best profit on the left with a lower price and the best profit on the right with a higher price. Add them to profit[j], track the max across all j, and return -1 if no j has both sides.
Do I need dynamic programming for this?+
Not really. A DP over increasing subsequences of length 3 works, but the middle-index approach is simpler. You only ever need exactly three picks, so two scans per index beat building a full LIS table.
How do I make it faster than O(n^2)?+
Compress the prices and use a Fenwick tree for prefix max of profit. Sweep left to right for the best lower-price profit, then sweep right to left with a mirrored tree for higher prices. That gives O(n log n) overall.
What edge cases break most solutions?+
Equal prices, since the inequality is strict. Fewer than three days. Returning 0 instead of -1 when nothing qualifies. Also negative-looking logic errors from initializing the best values to 0 instead of a sentinel like negative infinity.
How should I prepare in 48 hours for an Odoo OA like this?+
Practice the fix-the-middle pattern on a few array problems, then code this one from scratch with the O(n^2) version first. Test both examples, including the -1 case. Only optimize with a Fenwick tree if the constraints demand it.