How can you find two indices whose values sum to a target in one pass?
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
Keep a map from previously seen values to their indices. For each element, check whether its complement, target minus current value, has already appeared. Check before inserting so the same element cannot be used twice. This naturally handles duplicate values at different positions, such as two 3s for target 6. Expected time is O(n), with O(n) extra space under ordinary hash-map assumptions. State what to return when no pair exists and whether one pair or all pairs are required.
Related Questions in Data Structures & Algorithms
How to detect a cycle in a Linked List using Floyd's Cycle-Finding Algorithm?
Intermediate level • 574 views
When would you choose a hash map instead of an array for lookups?
Easy level • 70 views
How do you design a Distributed API Rate Limiter and what algorithms are commonly employed?
Advanced level • 62 views
What is Dynamic Programming? Compare Memoization (Top-Down) with Tabulation (Bottom-Up) using the 0/1 Knapsack Problem.
Advanced level • 60 views
Explain the LRU (Least Recently Used) Cache architecture and how to implement it in O(1) time complexity.
Advanced level • 59 views