Object Snap to Nearest Vertex Within Pick Tolerance

Last updated: November 29, 2026

Quick Overview

Given a large set of 2D vertices in a drawing and a pick tolerance, build a structure that answers many cursor queries: return the nearest vertex within tolerance of the cursor, or nothing if none is close enough.

Autodesk
Coding & Algorithms
Software Engineer
Autodesk
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a large set of 2D vertices in a drawing and a pick tolerance, build a structure that answers many cursor queries: return the nearest vertex within tolerance of the cursor, or nothing if none is close enough.

Object snap runs on every mouse move in AutoCAD and Fusion sketches, so it must answer in well under a frame. The interviewer checks whether the candidate moves from a linear scan to a spatial index and reasons correctly about which cells must be searched.

What the Interviewer Expects
  • Recognize brute force is O(n) per query and too slow at interactive rates
  • Use a uniform grid (spatial hash) with cell size tied to the tolerance
  • Search exactly the 3x3 neighborhood and justify why it suffices
  • Compare squared distances to avoid square roots
  • Deterministic tie-breaking
  • Discuss zoom-dependent tolerance and alternatives like k-d trees
Key Topics to Cover
Spatial hashing
Nearest neighbor
Grid indexing
Interactive latency
Floating point
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • The tolerance is in screen pixels, so it changes with zoom. How does that affect your index?
  • How would you support snapping to the nearest point on a segment, not just vertices?
  • Points are very unevenly distributed (a dense detail inside a huge site plan). Does the grid still work?
  • How do you update the index when entities move?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Build once from n vertices, then answer many snap(x, y) queries. Return the index of the closest vertex with distance at most tol, or None. Ties go to the lower index so results do not flicker. ...

High-Level Design

Hash each vertex into a grid cell of side tol. Any vertex within tol of the cursor differs by at most tol per axis, so it lies in the cursor's cell or one of the 8 neighbors. Search those 9 buck...


Submit Your Answer
Markdown supported

Related Questions