Coding Algorithms And Data Structures
Asked of: Data Scientist, Machine Learning Engineer
Last updated
What's being tested
Amazon MLE coding screens test whether you can turn ML-adjacent production tasks into clean algorithms: batching, clustering, training loops, graph reachability, interval allocation, and top-k retrieval. Interviewers look for correct data structures, complexity analysis, edge-case handling, and code that would survive inside a training or serving pipeline.
Patterns & templates
-
K-means implementation — initialize centroids, assign by nearest distance, recompute means; stop on max iterations or centroid shift .
-
Interval merging / allocation — sort by start time, scan once, merge or consume ranges; usually
O(n log n)time from sorting. -
Top-k frequency retrieval — use
collections.Counterplusheapq.nlargestforO(n log k), or bucket counts for bounded frequencies. -
Directed cycle check — adding edge
u -> vcreates a cycle iffuis reachable fromv; solve withDFS/BFS. -
PyTorch training loop — order matters:
model.train(), move tensors todevice,optimizer.zero_grad(), forward, loss,backward(),step(). -
Bucket batching optimization — sort or group examples by sequence length/cost, then pack batches to reduce padding and GPU underutilization.
-
Event-driven queues — model backorders or pending work with
deque,heapq, or ordered maps; define FIFO vs priority semantics explicitly.
Common pitfalls
Pitfall: Writing ML pseudocode without executable edge handling, such as empty clusters in K-means or zero-length batches in bucketing.
Pitfall: Missing complexity tradeoffs; Amazon interviewers expect
O(V+E),O(n log n), memory cost, and when the approach breaks at scale.
Pitfall: In PyTorch loops, forgetting
optimizer.zero_grad()or device movement silently produces wrong training behavior or runtime errors.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Implement Optimal Bucket BatchingAmazon · Machine Learning Engineer · Technical Screen · hard
- Solve two string DP/hash problemsAmazon · Data Scientist · Technical Screen · easy
- Find shortest transformation steps in a word graphAmazon · Machine Learning Engineer · Technical Screen · medium
- Find shortest path in a grid with obstaclesAmazon · Machine Learning Engineer · Onsite · medium
- Implement integer division without using divisionAmazon · Machine Learning Engineer · Onsite · medium
- Find two numbers that sum to targetAmazon · Machine Learning Engineer · Technical Screen · medium
- Design LFU cache with distributed extensionAmazon · Machine Learning Engineer · Onsite · medium
- Check if adding edge creates cycle in digraphAmazon · Machine Learning Engineer · Technical Screen · medium
- Solve two-sum variants at scaleAmazon · Data Scientist · Onsite · Medium
- Implement streaming k-way merge with constraintsAmazon · Data Scientist · Onsite · Medium
- Implement binary search lower/upper boundsAmazon · Machine Learning Engineer · Technical Screen · Medium
- Implement lower_bound and upper_bound binary searchAmazon · Machine Learning Engineer · Technical Screen · Medium
Related concepts
- Core Data Structures, Algorithms, And ComplexityCoding & Algorithms
- Arrays, Strings, Hash Maps, And Frequency CountingCoding & Algorithms
- Algorithms, Data Structures, And Complexity AnalysisCoding & Algorithms
- Core Data Structures, Sorting, And ComplexityCoding & Algorithms
- Arrays, Strings, and HashingCoding & Algorithms
- Array Search, Selection, And Dynamic ProgrammingCoding & Algorithms