In this lesson, Alvin explores the strategy to solving the following interview problem:
Write a function, pair_sum, that takes in a list and a target sum as arguments. The function should return a tuple containing a pair of indices whose elements sum to the given target. The indices returned must be unique.
Be sure to return the indices, not the elements themselves.
There is guaranteed to be one such pair that sums to the target.
pair_sum([3, 2, 5, 4, 1], 8) # -> (0, 2)
pair_sum([4, 7, 9, 2, 5, 1], 5) # -> (0, 5)
pair_sum([4, 7, 9, 2, 5, 1], 3) # -> (3, 5)
If you need additional support taking these DSA skills and actually applying them, take Alvin's complete data structures and algorithms course on Structy. You can try out the concepts yourself in their interactive code editor and learn advanced DSA patterns like stack exhaustive recursion.
Use this link to get 20% off the entire Structy DSA learning experience.
Follow Alvin on LinkedIn: https://www.linkedin.com/in/alvin-zablan-b73a92117/