Shortest Distance to Target Color (Preprocessing)

Data Structures
Medium
LinkedIn
96.8K views

Given an array of colors and a list of queries (index, color), find the shortest distance from the given index to the target color. Preprocessing with maps or arrays is key.

Why Interviewers Ask This

Interviewers at LinkedIn ask this to evaluate your ability to optimize time complexity through preprocessing. They want to see if you recognize that repeated queries on the same dataset require O(1) lookups rather than O(N) scans per query, demonstrating practical knowledge of space-time trade-offs and efficient data structure selection.

How to Answer This Question

Structure your response using a 'Clarify-Optimize-Implement' framework. First, clarify constraints: Ask if the array is static or dynamic and how many queries to expect to justify preprocessing. Second, propose the optimal strategy: Explain that scanning linearly for every query is too slow (O(Q*N)), so you should precompute distances. Suggest storing indices of each color in sorted lists or arrays. Third, detail the lookup logic: For a specific index and target color, use binary search on the stored indices to find the nearest occurrence efficiently. Fourth, discuss edge cases: What if the color doesn't exist? Handle null returns gracefully. Finally, analyze complexity: Confirm O(N) preprocessing and O(log N) or O(1) per query depending on implementation, showing you understand why this meets LinkedIn's scale requirements.

Key Points to Cover

  • Recognizing the need for preprocessing when facing repeated queries
  • Selecting appropriate data structures like Hash Maps with Sorted Lists
  • Applying Binary Search to minimize lookup time complexity
  • Explicitly calculating Time and Space complexity trade-offs
  • Handling edge cases where the target color is absent from the array

Sample Answer

To solve the Shortest Distance to Target Color problem efficiently, I would first analyze the query volume. If we have multiple queries, a naive O(N) scan per query becomes prohibitive. Instead, I recommend an O(N) prepr…

Common Mistakes to Avoid

  • Failing to preprocess and running a linear scan for every single query
  • Ignoring the case where the target color does not exist in the input array
  • Not considering the memory overhead of storing all indices for frequent colors
  • Overlooking the difference between finding any occurrence versus the nearest occurrence

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 162 Data Structures questionsBrowse all 26 LinkedIn questions