The Flipster software exercise asks for the maximum gain from one buy followed by one sell. State whether no trade is allowed, then explain how you would retain a reproducible result if the data feed arrived out of order.
What to prepare for Flipster
Flipster publishes API documentation. The example uses made-up price ticks and does not model real trading outcomes. Flipster API documentation
Track the best profit while scanning once
Given prices [8, 4, 9, 3, 6], return 5 if the task permits at most one ordered buy and sell. When there are no profitable pairs, return zero under the no-trade convention. Ask whether output must also identify indices. Related PracHub practice
def best_gain(prices):
minimum = None
result = 0
for price in prices:
if minimum is None:
minimum = price
continue
result = max(result, price - minimum)
minimum = min(minimum, price)
return result
assert best_gain([8, 4, 9, 3, 6]) == 5
assert best_gain([5, 4, 2]) == 0
assert best_gain([]) == 0
Maintain the minimum price before the current index and the best difference found so far. Evaluate a current sell against the prior minimum and then update the minimum. Runtime is O(n), with constant extra space. If you return indices, set stable tie rules, for example earliest buy then earliest sell.
Reason about timestamps before extending the algorithm
Mock observations arriving as t1, t3, t2 cannot be processed directly if the result depends on event order. Sort historical inputs and decide how repeated timestamps sequence. For streaming practice, specify allowed lateness and whether a late price can revise a published result. Clarify fees if the interviewer moves beyond the basic exercise.
Prepare for changed assumptions
Walk through decreasing prices, two equal best profits and the variant requiring exactly one trade. Show why the at-most-one result cannot simply be reused if a losing trade is mandatory.