Solve stock trading with pattern rules
Company: Jump Trading
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Take-home Project
Quick Answer: This set of problems evaluates algorithmic skills in pattern recognition, state management, and sequence processing, specifically testing pattern-based decision rules on time-series stock data and iterative substring elimination in string processing within the Coding & Algorithms domain.
Pattern-Triggered Stock Trading: Shares Held Each Day
Constraints
- 0 <= n == len(prices) <= 10^5
- Every consecutive pair of prices is strictly increasing or strictly decreasing (no equal adjacent prices).
- 1 <= len(buyPattern), len(sellPattern) <= n (when n >= 1)
- Each pattern entry is either 1 or -1.
- shares is never negative: a sell is only executed when at least one share is held.
Examples
Input: ([10, 9, 8, 7, 8, 9, 10, 11], [-1, -1], [1, 1])
Expected Output: [0, 0, 1, 2, 2, 1, 0, 0]
Explanation: Movements [-1,-1,-1,1,1,1,1]. Buys on days 2 and 3 (two downs), sells on days 5 and 6 (two ups); the would-be sell on day 7 is skipped because no shares are held.
Input: ([], [1], [1])
Expected Output: []
Explanation: Empty prices array yields an empty result.
Input: ([5], [1], [1])
Expected Output: [0]
Explanation: A single day has no movements, so no pattern can match; holdings stay 0.
Input: ([1, 2, 3], [-1, -1], [1, 1, 1])
Expected Output: [0, 0, 0]
Explanation: Movements [1,1]; buyPattern needs two downs (never occurs) and sellPattern needs three ups (too long). No actions.
Input: ([3, 4, 5], [-1], [1])
Expected Output: [0, 0, 0]
Explanation: Every movement is up, so sellPattern [1] matches but there are no shares to sell; buyPattern [-1] never matches.
Input: ([5, 4, 5, 4, 5], [-1], [1])
Expected Output: [0, 1, 0, 1, 0]
Explanation: Movements [-1,1,-1,1]. A down day buys, the next up day sells, alternating.
Input: ([10, 11, 12, 11, 10, 11, 12], [1, 1], [-1, -1])
Expected Output: [0, 0, 1, 1, 0, 0, 1]
Explanation: Movements [1,1,-1,-1,1,1]. buyPattern [1,1] triggers buys on days 2 and 6; sellPattern [-1,-1] triggers a sell on day 4.
Hints
- First convert prices into a movements array of +1/-1, then a pattern match on day i is just comparing a fixed-length slice of movements ending at day i.
- Apply the buy rule before the sell rule on the same day, and clamp at zero so you never sell shares you do not hold.
- A pattern of length L cannot match until at least L movements (i.e. day index >= L) are available.
Repeatedly Remove Combo Substrings Until Stable
Constraints
- 0 <= len(s)
- 1 <= len(combos); each combo is a non-empty string.
- Removals cascade: deleting a combo may create a new combo at the junction, which must also be removed.
- The stack rule (longer combos checked first on the suffix) makes the result deterministic even when combos overlap.
Examples
Input: ("abxabccba", ["aa", "bb", "cc"])
Expected Output: "abx"
Explanation: ...cc collapses, then the exposed bb, then aa, leaving "abx".
Input: ("aaabbbccc", ["aa", "bb", "cc"])
Expected Output: "abc"
Explanation: Each run of three equal letters loses one matching pair, leaving one of each: "abc".
Input: ("dabcd", ["abc"])
Expected Output: "dd"
Explanation: Removing "abc" from the middle leaves the two surrounding d's: "dd".
Input: ("xyyx", ["yy", "xx"])
Expected Output: ""
Explanation: "yy" is removed first, exposing "xx", which is then removed, leaving the empty string.
Input: ("", ["ab"])
Expected Output: ""
Explanation: An empty input string yields an empty result.
Input: ("hello", ["xy"])
Expected Output: "hello"
Explanation: No combo appears, so the string is returned unchanged.
Input: ("aabaa", ["aa"])
Expected Output: "b"
Explanation: Both "aa" pairs are removed, leaving only the middle "b".
Input: ("abccba", ["cc", "bb", "aa"])
Expected Output: ""
Explanation: "cc" removed exposes "bb", then "aa"; the whole string cancels to empty.
Input: ("mississippi", ["ss", "pp"])
Expected Output: "miiii"
Explanation: Both "ss" and the "pp" are removed, leaving "miiii".
Hints
- Maintain a stack of characters; after pushing each character, only the suffix of the stack can newly form a combo, so you never need to rescan the whole string.
- After a removal, immediately re-check the new suffix — one deletion can expose another combo at the seam.
- Check longer combos before shorter ones so the reduction is well-defined when combo strings overlap.