← Deshaw Interview Insights

Deshaw·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Two coding problems for a Machine Learning Engineer role at Deshaw. Nothing ML-specific, just pure algorithms. The problems themselves weren't brutal but the in-place constraint on the second one made me second-guess myself more than I'd like to admit.

Questions Asked (2)

Q1

Implement a square root function from scratch, without using any math library functions. It should handle edge cases like zero and very large or very small inputs, and stay within a specified precision bound.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Binary search approach felt natural here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements: precision bound, input range, and whether negative inputs need handling. Then implement Newton's method (or binary search) with careful initialization and iteration control, and validate with edge cases like 0, very large/small numbers, and perfect squares.

Pro tip: Mention that Newton's method converges quadratically, so you can set a fixed iteration count based on the precision bound (e.g., 50 iterations for double precision) to avoid infinite loops and ensure predictable performance.

1. Clarify requirements and edge cases

Ask about precision bound, input domain (non-negative?), and expected performance. Identify edge cases: 0, 1, very large (e.g., 1e300), very small (e.g., 1e-300), and perfect squares.

2. Choose an algorithm

Select Newton's method for fast convergence or binary search for simplicity and robustness. Explain trade-offs: Newton's is faster but needs a good initial guess; binary search is slower but always works.

3. Handle initialization and scaling

For Newton's, initialize x0 = n or use bit manipulation to get a close guess. For very large/small numbers, scale the input to a safe range (e.g., using exponent manipulation) to avoid overflow/underflow.

4. Implement iteration with stopping criterion

Iterate until the change is below the precision bound or a max iteration count is reached. Use relative error for large numbers and absolute error for small numbers.

5. Test and validate

Test with edge cases and compare against known values. Ensure the result squared is within the precision bound of the input.

Key Points to Mention

  • Newton's method formula: x_{n+1} = 0.5 * (x_n + n / x_n)
  • Convergence rate: quadratic, so few iterations needed for double precision
  • Handling zero: return 0 immediately
  • Handling negative inputs: either return NaN or raise an error, depending on requirements
  • Scaling for very large/small numbers to avoid overflow/underflow
  • Precision bound: use relative error for large numbers, absolute for small
  • Alternative: binary search for guaranteed convergence and simplicity
  • Initial guess: use bit manipulation or a simple heuristic to speed up convergence

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Rearrange an integer array in-place so that all elements at even positions (1-based) are strictly less than all elements at odd positions. Only O(1) extra space allowed.

Algorithms & Data Structures
Author's notes

Sort the array first, then slot the smaller half into even indices and the larger half into odd indices.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that 'even positions' and 'odd positions' refer to 1-based indexing, so even positions are indices 0,2,4,... and odd positions are 1,3,5,... in 0-based arrays. Then propose sorting the array in-place using a comparison sort like heapsort, which uses O(1) extra space, and finally swap adjacent elements starting from index 0 to ensure all even-position elements are less than all odd-position elements. Alternatively, use a median-based partition (quickselect) to achieve O(n) time on average, but sorting is simpler and guarantees O(n log n) time.

Pro tip: Mention that after sorting, swapping adjacent pairs (0,1), (2,3), etc., works because the sorted order guarantees each even-position element is less than the next odd-position element, and since the array is sorted, all even-position elements are less than all odd-position elements. Also, note that if the array has duplicate values, strict inequality may fail, so discuss handling duplicates or assume distinct elements.

1. Clarify indexing and constraints

Confirm that positions are 1-based, so even positions correspond to 0-based even indices. Restate the O(1) space constraint and ask if the array can contain duplicates.

2. Choose an in-place sorting algorithm

Select an O(1) space sorting algorithm such as heapsort (O(n log n) time) or quickselect-based partitioning (O(n) average time) to rearrange elements.

3. Sort the array in-place

Apply the chosen algorithm to sort the array in non-decreasing order. This ensures that for any i < j, arr[i] <= arr[j].

4. Swap adjacent pairs

Iterate from index 0 to n-2 with step 2, swapping arr[i] and arr[i+1]. This places the smaller element at the even position and the larger at the odd position.

5. Verify and handle duplicates

Check that all even-position elements are strictly less than all odd-position elements. If duplicates exist, discuss that strict inequality may not hold and propose modifications (e.g., using a stable partition or assuming distinct values).

Key Points to Mention

  • 1-based indexing clarification: even positions = 0-based even indices.
  • O(1) extra space constraint rules out merge sort and standard quicksort (due to recursion stack).
  • Heapsort is a suitable in-place O(n log n) sorting algorithm.
  • After sorting, swapping adjacent pairs (0,1), (2,3), ... achieves the required property.
  • Time complexity: O(n log n) with heapsort; O(n) average with quickselect-based partition.
  • Handling duplicates: strict inequality may fail; discuss assumptions or alternative approaches.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.