Implement drawdown, single-trade profit, coin change
Company: Squarepoint Capital
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's algorithmic problem-solving skills across array and time-series processing, greedy optimization, and dynamic programming, requiring implementation and complexity analysis of maximum drawdown, single-trade profit, and coin change problems within the Coding & Algorithms domain for a Data Scientist role.
Maximum Drawdown of a Cumulative PnL Series
Constraints
- 0 <= len(pnl) <= 10^5
- -10^9 <= pnl[i] <= 10^9
- Drawdown is measured relative to the running maximum (peak) seen so far.
- If the series never falls below its running peak, the drawdown is 0.
Examples
Input: ([0, 5, 3, 8, 2, 6, 1],)
Expected Output: (-7, 3, 6)
Explanation: Running peak hits 8 at index 3; series bottoms at 1 (index 6): 1 - 8 = -7.
Input: ([1, 2, 3, 4, 5],)
Expected Output: (0, 0, 0)
Explanation: Monotonically increasing series never dips below its running peak, so drawdown is 0.
Input: ([5, 4, 3, 2, 1],)
Expected Output: (-4, 0, 4)
Explanation: Peak is 5 at index 0; trough is 1 at index 4: 1 - 5 = -4.
Input: ([],)
Expected Output: (0, -1, -1)
Explanation: Empty series: defined to return (0, -1, -1).
Input: ([10],)
Expected Output: (0, 0, 0)
Explanation: Single point cannot draw down from itself.
Input: ([3, 1, 4, 1, 5, 9, 2, 6],)
Expected Output: (-7, 5, 6)
Explanation: Peak 9 at index 5, then 2 at index 6: 2 - 9 = -7, the worst single drop.
Hints
- Sweep once from left to right, tracking the highest cumulative PnL (and its index) seen so far.
- At each step the current drawdown is value - runningPeak; remember the peak's index so you can report where the drop began.
- Keep the most negative drawdown observed along with the peak index that produced it and the current index as the trough.
Best Time to Buy and Sell Stock (Single Trade)
Constraints
- 0 <= len(prices) <= 10^5
- 0 <= prices[i] <= 10^4
- You must buy before you sell (buy day strictly before sell day).
- Only a single buy/sell transaction is allowed.
Examples
Input: ([7, 1, 5, 3, 6, 4],)
Expected Output: 5
Explanation: Buy at 1 (day 1), sell at 6 (day 4): profit 5.
Input: ([7, 6, 4, 3, 1],)
Expected Output: 0
Explanation: Prices only fall; no profitable trade, so return 0.
Input: ([1, 2, 3, 4, 5],)
Expected Output: 4
Explanation: Buy at 1, sell at 5: profit 4.
Input: ([],)
Expected Output: 0
Explanation: Empty input: no trade possible.
Input: ([5],)
Expected Output: 0
Explanation: Only one day; cannot buy and sell on different days.
Input: ([2, 2, 2, 2],)
Expected Output: 0
Explanation: Flat prices yield no profit.
Input: ([3, 8, 1, 9],)
Expected Output: 8
Explanation: Buy at 1 (day 2), sell at 9 (day 3): profit 8, which beats 8 - 3 = 5.
Hints
- As you scan left to right, track the lowest price seen so far — that is the best day to have bought before today.
- At each day, the best profit if you sold today is price - minSoFar; keep the maximum across all days.
- If prices only ever fall, the running best profit stays 0, which is the correct answer.
Coin Change (Minimum Coins)
Constraints
- 1 <= len(coins) <= 12
- 1 <= coins[i] <= 2^31 - 1
- 0 <= amount <= 10^4
- Unlimited supply of each coin denomination.
Examples
Input: ([1, 2, 5], 11)
Expected Output: 3
Explanation: 11 = 5 + 5 + 1 uses 3 coins, the minimum.
Input: ([2], 3)
Expected Output: -1
Explanation: Odd target with only a coin of 2 is impossible.
Input: ([1], 0)
Expected Output: 0
Explanation: Amount 0 needs no coins.
Input: ([2, 5, 10, 1], 27)
Expected Output: 4
Explanation: 27 = 10 + 10 + 5 + 2 uses 4 coins.
Input: ([186, 419, 83, 408], 6249)
Expected Output: 20
Explanation: Greedy fails here; DP finds 20 coins as the true minimum.
Input: ([5], 5)
Expected Output: 1
Explanation: A single coin of 5 makes 5.
Input: ([7, 11], 1)
Expected Output: -1
Explanation: No combination of 7 and 11 forms 1.
Hints
- Let dp[a] = the minimum number of coins to form amount a; dp[0] = 0.
- For each amount a, try every coin c <= a and relax dp[a] = min(dp[a], dp[a - c] + 1).
- Initialize unreachable amounts to a sentinel (e.g. amount + 1); if dp[amount] is still the sentinel at the end, return -1.