Solve stock trading with pattern rules
Problem Description
You are tasked with simulating a stock trading strategy based on predefined price movement patterns.
You will be provided with:
- An array
pricesrepresenting the daily closing prices of a stock. - A
sellPatternarray, consisting of1(price increased) and-1(price decreased) values. This pattern defines a sequence of consecutive daily price movements that, if matched, triggers a sell action. - A
buyPatternarray, similarly consisting of1and-1values, defining a sequence of consecutive daily price movements that, if matched, triggers a buy action.
A price movement 1 signifies that prices[k] > prices[k-1]. A price movement -1 signifies that prices[k] < prices[k-1]. If prices[k] == prices[k-1], it does not match either 1 or -1.
For each day i from 0 to n-1 (where n is the number of days/prices), you must determine the number of shares held. The decision to buy or sell is made based on the patterns matching the price movements immediately preceding and ending on day i.
The trading rules are as follows:
- Buy Action: If the
buyPatternmatches the sequence of price movements ending on dayi, AND thesellPatterndoes NOT match, then one share is bought. - Sell Action: If the
sellPatternmatches the sequence of price movements ending on dayi, AND thebuyPatterndoes NOT match, then one share is sold (provided the number of shares held is greater than zero). - No Change: If both
buyPatternandsellPatternmatch, or if neither pattern matches, the number of shares held remains unchanged.
You start with 0 shares. The number of shares cannot go below zero.
Deliverable
Return an integer array shares of length n, where shares[i] represents the total number of shares held after processing day i.
Example
prices = ?(actual prices not provided in source, but implied)sellPattern = [1,1](e.g., price up, then up again)buyPattern = [...](e.g., price down, then down again)expected output = [0,0,1,2,1,0](demonstrates share accumulation and reduction)
AI interview prep
Core dilemma
Efficiently identifying dynamic patterns in a streaming sequence of data (price movements) and making stateful decisions based on these patterns, while handling edge cases like insufficient shares and ambiguous pattern matching conditions.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve stock trading with pattern rules
Problem Description
You are tasked with simulating a stock trading strategy based on predefined price movement patterns.
You will be provided with:
- An array
pricesrepresenting the daily closing prices of a stock. - A
sellPatternarray, consisting of1(price increased) and-1(price decreased) values. This pattern defines a sequence of consecutive daily price movements that, if matched, triggers a sell action. - A
buyPatternarray, similarly consisting of1and-1values, defining a sequence of consecutive daily price movements that, if matched, triggers a buy action.
A price movement 1 signifies that prices[k] > prices[k-1]. A price movement -1 signifies that prices[k] < prices[k-1]. If prices[k] == prices[k-1], it does not match either 1 or -1.
For each day i from 0 to n-1 (where n is the number of days/prices), you must determine the number of shares held. The decision to buy or sell is made based on the patterns matching the price movements immediately preceding and ending on day i.
The trading rules are as follows:
- Buy Action: If the
buyPatternmatches the sequence of price movements ending on dayi, AND thesellPatterndoes NOT match, then one share is bought. - Sell Action: If the
sellPatternmatches the sequence of price movements ending on dayi, AND thebuyPatterndoes NOT match, then one share is sold (provided the number of shares held is greater than zero). - No Change: If both
buyPatternandsellPatternmatch, or if neither pattern matches, the number of shares held remains unchanged.
You start with 0 shares. The number of shares cannot go below zero.
Deliverable
Return an integer array shares of length n, where shares[i] represents the total number of shares held after processing day i.
Example
prices = ?(actual prices not provided in source, but implied)sellPattern = [1,1](e.g., price up, then up again)buyPattern = [...](e.g., price down, then down again)expected output = [0,0,1,2,1,0](demonstrates share accumulation and reduction)