Ctrl K

Dinner for Two

Easy
Two friends are choosing a place for dinner. Each wrote down their own list of places in order of preference: first belongs to one friend, second to the other. Position 0 holds the favorite. For a place that appears in both lists, add up its two positions: its index in first plus its index in second. The smaller the sum, the better the compromise. Return every shared place with the smallest position sum, in any order.
Example 1
first=['Pizza', 'Sushi', 'Tacos', 'Ramen'], second=['Ramen', 'Burgers', 'Sushi'] → ['Sushi', 'Ramen']

The shared places are "Sushi" (1 + 2 = 3) and "Ramen" (3 + 0 = 3). The sums tie, so both qualify.

Example 2
first=['Bakery', 'Deli', 'Noodle Bar'], second=['Deli', 'Noodle Bar'] → ['Deli']

"Deli" gives 1 + 0 = 1 and "Noodle Bar" gives 2 + 1 = 3, so "Deli" is the only answer.

Example 3
first=['Grill'], second=['Cafe', 'Grill'] → ['Grill']

There is just one shared place, so it is the answer.

Constraints
  • 1 ≤ len(first), len(second) ≤ 1000
  • 1 ≤ length of each name ≤ 30
  • Names consist of English letters and spaces
  • Names do not repeat within a list
  • At least one place appears in both lists
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsOne-Row Typing EasyPhrases with Synonyms EasyWord from a License Plate EasyDistinct Morse Signals EasyReal Inboxes Behind the Addresses Easy
loading Python…
Input
first=['Pizza', 'Sushi', 'Tacos', 'Ramen'], second=['Ramen', 'Burgers', 'Sushi']
Expected
['Sushi', 'Ramen']

Run checks the first 3 tests in your browser. Submit checks all 90 tests on the server, hidden ones included.