Find the common strings of two lists with the smallest index sum

Quick Overview

Given two lists of unique strings, return every string that appears in both lists with the smallest sum of its two positions. The problem tests hash map lookups, careful handling of ties and output order, and a linear-time scan over lists of up to 1,000 entries.

Find the common strings of two lists with the smallest index sum

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given two arrays of strings `list1` and `list2`, find the common strings with the least index sum. A common string is a string that appears in both `list1` and `list2`. If a common string appears at `list1[i]` and `list2[j]`, its index sum is `i + j`. A common string has the least index sum when no other common string has a smaller index sum. Return all the common strings with the least index sum. ### Function Signature ```python def find_least_index_sum_common(list1: list[str], list2: list[str]) -> list[str]: ``` ### Rules - Indices are 0-based. - Strings are compared exactly, including case and spaces. - When several common strings tie for the least index sum, return all of them in the order they appear in `list1`. ### Constraints - `1 <= list1.length, list2.length <= 1000` - `1 <= list1[i].length, list2[i].length <= 30` - `list1[i]` and `list2[i]` consist of English letters (upper and lower case), digits and spaces. - All the strings of `list1` are unique. - All the strings of `list2` are unique. - There is at least one common string between `list1` and `list2`. ### Examples ```text Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"] Output: ["Shogun"] ``` The only common string is "Shogun", with index sum `0 + 3 = 3`. ```text Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"] Output: ["Shogun"] ``` "Shogun" has index sum `0 + 1 = 1`, "KFC" has `3 + 0 = 3`, and "Burger King" has `2 + 2 = 4`. The minimum index sum is 1, so the answer is `["Shogun"]`. ```text Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"] Output: ["happy","sad"] ``` "happy" has index sum `0 + 1 = 1` and "sad" has `1 + 0 = 1`, while "good" has `2 + 2 = 4`. Two strings tie at 1, and "happy" comes first in `list1`.

Overview: Given two lists of unique strings, return every string that appears in both lists with the smallest sum of its two positions. The problem tests hash map lookups, careful handling of ties and output order, and a linear-time scan over lists of up to 1,000 entries.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given two arrays of strings list1 and list2, find the common strings with the least index sum.

A common string is a string that appears in both list1 and list2. If a common string appears at list1[i] and list2[j], its index sum is i + j. A common string has the least index sum when no other common string has a smaller index sum.

Return all the common strings with the least index sum.

Function Signature

def find_least_index_sum_common(list1: list[str], list2: list[str]) -> list[str]:

Rules

  • Indices are 0-based.
  • Strings are compared exactly, including case and spaces.
  • When several common strings tie for the least index sum, return all of them in the order they appear in list1 .

Constraints

  • 1 <= list1.length, list2.length <= 1000
  • 1 <= list1[i].length, list2[i].length <= 30
  • list1[i] and list2[i] consist of English letters (upper and lower case), digits and spaces.
  • All the strings of list1 are unique.
  • All the strings of list2 are unique.
  • There is at least one common string between list1 and list2 .

Examples

Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]

The only common string is "Shogun", with index sum 0 + 3 = 3.

Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
Output: ["Shogun"]

"Shogun" has index sum 0 + 1 = 1, "KFC" has 3 + 0 = 3, and "Burger King" has 2 + 2 = 4. The minimum index sum is 1, so the answer is ["Shogun"].

Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
Output: ["happy","sad"]

"happy" has index sum 0 + 1 = 1 and "sad" has 1 + 0 = 1, while "good" has 2 + 2 = 4. Two strings tie at 1, and "happy" comes first in list1.