Robust Coding And Numeric Edge Cases
Asked of: Data Scientist
Last updated

What's being tested
These problems test numeric stability and robust handling of edge cases in algorithmic code: correct use of analytic vs iterative solvers, safe aggregation over missing/empty data, and cluster-maintenance strategies. Interviewers expect concise, production-ready code that handles NaNs, extreme values, degenerate geometry, and streaming inputs.
Patterns & templates
-
Pairwise quadratic solver for moving-object collisions — solve as , handle discriminant , use
math.isclosetolerances. -
Use analytic formulas where stable; prefer
math.fsumoversumfor large numerics to avoid loss of precision. -
Welford's algorithm for streaming mean/variance — single pass, O(1) memory, numerically stable incremental updates.
-
For empty/missing values, prefer
numpy.nanmeanor explicit checks withmath.isnan/math.isfiniteand return0or sentinel per spec. -
Root-finding fallbacks: use
bisect(guaranteed convergence) whenNewton's method diverges; always cap iterations and check derivative magnitude. -
K-means empty-cluster handling: re-seed with farthest point, split largest cluster, or use minibatch to avoid empties; document deterministic tie-breaks.
-
Spatial pruning: use grid hashing / KD-tree / sweep-line to reduce pair checks to near-linear in sparse scenarios.
-
Use relative vs absolute tolerance: compare with (implement via
math.isclose).
Common pitfalls
Pitfall: Treating NaN or infinite values as numbers — forgetting
math.isfiniteleads to silent wrong answers or crashes.
Pitfall: Using naive
sumfor long lists — leads to catastrophic cancellation; prefermath.fsumor compensated summation.
Pitfall: Returning any root from quadratic without checking time bounds or negative times — report earliest non-negative collision only.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Implement a Safe Average Function in PythonWaymo · Data Scientist · Take-home Project · medium
- Implement Safe Average FunctionWaymo · Data Scientist · Take-home Project · medium
- Determine earliest collision among moving carsWaymo · Data Scientist · Technical Screen · easy
- Implement K-means and handle train-inference mismatchWaymo · Data Scientist · Technical Screen · easy
Related concepts
- Clean Coding, Requirements, and Edge CasesCoding & Algorithms
- Algorithms, Data Structures, And Complexity AnalysisCoding & Algorithms
- Arrays, Strings, Hash Maps, And Frequency CountingCoding & Algorithms
- Coding Algorithms And Data StructuresCoding & Algorithms
- Arrays, Strings, And Matrix FundamentalsCoding & Algorithms
- Coding Fundamentals For Data Scientists