My first instinct was a brute force double loop and I almost just went with it.
Use a hash map to store each task's duration and its index as you iterate through the array. For each task, check if its complement (60 - duration) exists in the map; if so, record the index pair. This yields O(n) time and O(n) space.
Pro tip: Clarify whether each task can be used in multiple pairs or if pairs should be unique, and whether the output order matters. Also, mention that you'd handle duplicates carefully by storing a list of indices for each duration.
Ask about edge cases: can a task be paired with itself? Should pairs be unique? What if multiple tasks have the same duration? Does the order of pairs matter?
Select a hash map (dictionary) to map duration to a list of indices, enabling O(1) complement lookups.
Traverse the array; for each task, compute its complement and check if it exists in the map. If yes, form pairs with all stored indices (avoiding self-pairing) and add to results. Then add the current index to the map.
Ensure that when the complement equals the current duration, you only pair with previously seen indices to avoid duplicates and self-pairing.
State time complexity O(n) and space O(n). Walk through the example and test edge cases like empty array, no pairs, and multiple pairs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.