Minimum Index Sum of Two Lists

EasyArrays & Hashinghashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Two friends each list their favorite restaurants as arrays of strings list1 and list2, ordered from most to least favorite. Neither list contains duplicates. A common string is one that appears in both lists. For a common string at list1[i] and list2[j], its index sum is i + j. Return every common string whose index sum is the minimum among all common strings. The lists are guaranteed to share at least one string. The answer may be returned in any order.

Examples

Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
"Shogun" is the only common string.
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
Output: ["Shogun"]
"Shogun" has index sum 0 + 1 = 1; "Burger King" has 4 and "KFC" has 3.
Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
Output: ["sad","happy"]
"happy" and "sad" both have index sum 1; "good" has 4.

Constraints

  • 1 <= list1.length, list2.length <= 1000
  • 1 <= list1[i].length, list2[i].length <= 30
  • `list1[i]` and `list2[i]` consist of spaces and English letters
  • Strings within each list are unique
  • At least one string appears in both lists

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems