Magicsheet logo

Minimum Index Sum of Two Lists

Easy
97.5%
Updated 6/1/2025

Minimum Index Sum of Two Lists

1. What is this problem about?

The "Minimum Index Sum of Two Lists" coding problem is a popular "easy" difficulty question often used in the initial rounds of interviews. You are given two lists of strings (e.g., restaurant names), and you need to find the strings that are common to both lists. However, there's a catch: you aren't just looking for any common string; you are looking for those with the minimum sum of their indices in the two lists.

If multiple strings share the same minimum index sum, you should return all of them. This problem tests your ability to efficiently cross-reference data and keep track of a "running minimum" while processing potential candidates.

2. Why is this asked in interviews?

Yelp and Amazon frequently use this question because it mirrors a real-life use case: finding common preferences between two users. If User A lists their favorite restaurants in order and User B does the same, the restaurant they both like that is "highest" on their combined lists (lowest index sum) is the best recommendation.

Interviewer's use this to check:

  • Data Structure Selection: Does the candidate use a Hash Map for fast lookups?
  • Handling Multi-item Results: Can the candidate correctly manage a list of results when several items tie for the minimum?
  • String Comparison Efficiency: Understanding that while strings are being compared, the primary logic is about index manipulation.

3. Algorithmic pattern used

The most efficient pattern is the Hash Map (Hash Table) pattern.

  1. Store all strings from the first list as keys in a hash map, with their indices as the values.
  2. Iterate through the second list. For each string, check if it exists in the hash map.
  3. If it exists, calculate the index sum (current index in list 2 + stored index from list 1).
  4. Maintain a variable for the current_min_sum.
    • If the new sum is less than current_min_sum, clear your result list, add the current string, and update current_min_sum.
    • If the new sum equals current_min_sum, simply add the string to your result list.

4. Example explanation

List 1: ["Burger King", "KFC", "Subway", "Pizza Hut"] List 2: ["KFC", "Burger King", "Tapioca Express"]

  • "KFC" is at index 1 in List 1 and index 0 in List 2. Sum = 1+0=11 + 0 = 1.
  • "Burger King" is at index 0 in List 1 and index 1 in List 2. Sum = 0+1=10 + 1 = 1.
  • "Subway" is not in List 2.
  • "Pizza Hut" is not in List 2.
  • "Tapioca Express" is not in List 1.

The minimum sum found is 1. Both "KFC" and "Burger King" result in this sum. The final answer would be ["KFC", "Burger King"].

5. Common mistakes candidates make

  • Nested Loops: Using two for-loops to compare every item in List 1 with every item in List 2. This results in O(M×N)O(M \times N) time complexity, which is inefficient for large lists.
  • Overwriting the Result: Forgetting to clear the result list when a new lower minimum index sum is found.
  • Case Sensitivity: Not asking if "kfc" and "KFC" should be treated as the same restaurant (usually they are different unless specified).

6. Interview preparation tip

When you see a problem involving "finding common elements" or "intersection," your mind should immediately jump to Hash Maps. They provide O(1)O(1) average time complexity for lookups, which usually turns an O(N2)O(N^2) problem into an O(N)O(N) one. Always clarify the output format—do the results need to be in any specific order? (In this problem, they usually don't).

Similar Questions