Implement EMA Crossovers and Root Solvers
Company: Squarepoint
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates time-series data manipulation and vectorized pandas operations for EMA crossovers, numerical methods and error analysis for implementing a square-root approximation, combinatorial reasoning for counting polygon diagonals, and state-space problem-solving for the classic water-jug reachability puzzle.
Part 1: Detect EMA Crossovers
Constraints
- 0 <= len(timestamps) == len(prices) <= 200000
- 1 <= short_span, long_span <= 10^6
- timestamps are already sorted in increasing order
- If there are fewer than 2 price points, there can be no crossover
Examples
Input: ([1, 2, 3, 4, 5], [10.0, 9.0, 11.0, 8.0, 12.0], 2, 3)
Expected Output: [[3, 5], [2, 4]]
Explanation: The short EMA drops below at timestamp 2, rises above at 3, drops below at 4, and rises above again at 5.
Input: ([], [], 2, 3)
Expected Output: [[], []]
Explanation: Empty input has no crossovers.
Input: ([1], [100.0], 2, 5)
Expected Output: [[], []]
Explanation: A single observation cannot create a crossover.
Input: ([1, 2, 3, 4], [1.0, 2.0, 3.0, 4.0], 2, 4)
Expected Output: [[2], []]
Explanation: Both EMAs start equal. The short EMA moves above the long EMA at timestamp 2 and never crosses back.
Input: ([1, 2, 3], [5.0, 5.0, 5.0], 2, 3)
Expected Output: [[], []]
Explanation: Equal prices keep both EMAs equal, so no crossover occurs.
Hints
- You do not need to store all EMA values. Each new EMA only depends on the previous EMA and the current price.
- Track the sign of short_ema - long_ema. A crossover occurs when that sign changes across consecutive observations.
Part 2: Approximate Square Root with Precision
Constraints
- 0 <= x <= 10^12
- 10^-6 <= eps <= 1
- Do not use math.sqrt or any other built-in square root routine
Examples
Input: (0, 1e-6)
Expected Output: 0.0
Explanation: The square root of 0 is exactly 0.
Input: (1, 1e-6)
Expected Output: 1.0
Explanation: The square root of 1 is exactly 1.
Input: (2, 1e-6)
Expected Output: 1.414214
Explanation: This is the square root of 2 rounded to 6 decimal places.
Input: (0.25, 1e-6)
Expected Output: 0.5
Explanation: This checks a value between 0 and 1.
Input: (1000000, 1e-6)
Expected Output: 1000.0
Explanation: A large perfect square should still be handled correctly.
Hints
- On non-negative numbers, the function f(m) = m * m is monotonic, so binary search works.
- If x is less than 1, the square root still lies between 0 and 1.
Part 3: Count Polygon Diagonals
Constraints
- 3 <= n <= 10^9
- Use 64-bit arithmetic in languages with fixed-size integers
Examples
Input: 3
Expected Output: 0
Explanation: A triangle has no diagonals.
Input: 4
Expected Output: 2
Explanation: A quadrilateral has two diagonals.
Input: 5
Expected Output: 5
Explanation: A pentagon has five diagonals.
Input: 10
Expected Output: 35
Explanation: Using the formula n * (n - 3) / 2 gives 10 * 7 / 2 = 35.
Input: 100000
Expected Output: 4999850000
Explanation: This tests large input and the need for 64-bit arithmetic.
Hints
- From one vertex, you can connect to n - 3 vertices by diagonals because you must exclude the vertex itself and its two neighbors.
- If you count diagonals from every vertex, you count each diagonal twice.
Part 4: Measure an Exact Target with Two Jugs
Constraints
- 1 <= cap_a, cap_b <= 100
- 0 <= target <= max(cap_a, cap_b)
- The start state is always (0, 0)
Examples
Input: (3, 5, 4)
Expected Output: ['fill B', 'pour B A', 'empty A', 'pour B A', 'fill B', 'pour B A']
Explanation: This is the classic 3-gallon and 5-gallon puzzle. The final state reached is (3, 4).
Input: (3, 5, 0)
Expected Output: []
Explanation: No action is needed to have 0 gallons.
Input: (2, 4, 3)
Expected Output: []
Explanation: It is impossible because 3 is not divisible by gcd(2, 4).
Input: (3, 5, 5)
Expected Output: ['fill B']
Explanation: The target already matches the capacity of jug B.
Input: (2, 3, 1)
Expected Output: ['fill B', 'pour B A']
Explanation: Filling B and pouring into A leaves exactly 1 gallon in jug B.
Hints
- Think of each water state (a, b) as a node in a graph, and each allowed move as an edge.
- Breadth-first search gives a shortest sequence of moves. A gcd check can rule out impossible cases early.