First item bought twice

hashing

First item bought twice

Instacart Python Interview Question

Instacart suggests a "buy it again" item as soon as a shopper adds something to their cart for the second time.

Write a function first_repeat_item(items) that takes the items in the order they were added and returns the first item that is added a second time. Return None if no item is added twice.

Asked of

  • Data Engineer
  • Data Scientist
  • Analytics Engineer
  • ML Engineer
  • AI Engineer

Example 1

Input

first_repeat_item(["milk", "eggs", "bread", "eggs", "milk"])

Output

"eggs"

Example 2

Input

first_repeat_item(["apples", "bananas"])

Output

None

Explanation

In the first example, eggs are added again as the fourth item, before milk is added again as the fifth, so the answer is eggs. In the second example, apples and bananas are each added once, so the answer is None.

Submit also runs 3 hidden test cases that check edge cases.