You are interviewing for a storage/infra-leaning role. Solve the following coding tasks and be ready to discuss time/space complexity and edge cases.
Task A: Max profit from one trade
Given an integer array prices where prices[i] is the price of a stock on day i, compute the maximum profit achievable by choosing at most one buy day b and one sell day s with b < s. If no profit is possible, return 0.
-
Input:
prices: int[]
-
Output:
max_profit: int
-
Constraints (typical):
1 <= n <= 2e5
, prices fit in 32-bit int
Follow-ups:
-
What is the best achievable time and space complexity?
-
How do you handle monotonically decreasing prices?
Task B: Right-side view of a binary tree
Given the root of a binary tree, return the list of node values visible when the tree is viewed from the right side (from top level to bottom level).
-
Input:
root
(binary tree node)
-
Output:
right_view: int[]
Follow-ups:
-
Compare DFS vs BFS approaches and their complexities.
-
What are edge cases (empty tree, skewed tree, very deep tree)?
Discussion (no coding required, but explain clearly)
-
If the code were used in a
multi-threaded
environment or under
very large datasets
, what assumptions break and what changes (if any) would you make?