Frontend School

Find Common Items in Two Customer Wishlists

DSA
easy

Find Common Items in Two Customer Wishlists

Easy
Sorting
Searching
Arrays
Two Pointers

Overview

At Amazon, understanding customer preferences is key to improving the shopping experience. One way we do this is by analyzing customer wishlists. Imagine a scenario where we want to identify items that are popular across different customer segments or find potential bundle opportunities. This problem focuses on a core part of that analysis: finding common items between two distinct customer wishlists.

What you need to do / Task

Your task is to implement a function findCommonItems that takes two arrays of item IDs, representing two customer wishlists. The function should return a new array containing all item IDs that are present in both wishlists. The resulting array of common items must be:

  1. Sorted in ascending order.
  2. Contain only unique item IDs (even if an item appears multiple times in the input wishlists, it should only appear once in the result).

Input & output

  • Input:
    • wishlist1: An array of numbers. Each number represents an item ID that a customer has added to their first wishlist.
    • wishlist2: An array of numbers. Each number represents an item ID that a customer has added to their second wishlist.
  • Output: An array of numbers. This array should contain all item IDs that are present in both wishlist1 and wishlist2. The output array must be sorted in ascending order and contain only unique item IDs.

Clarifications

  • Empty Wishlists: If either wishlist1 or wishlist2 is empty, the function should return an empty array, as there can be no common items.
  • Duplicate Items in Input: Input wishlists may contain duplicate item IDs (e.g., [1, 5, 5, 10]). Your output should still contain only unique common items.
  • Item ID Range: Item IDs are positive integers.

Examples

1

Example 1

Input:

wishlist1 = [1, 5, 2, 8, 5], wishlist2 = [5, 2, 9, 10, 5]

Output:

[2,5]

Note:

Items 2 and 5 are present in both wishlists. Even though 5 appears multiple times, it's listed once. The result is sorted.

2

Example 2

Input:

wishlist1 = [10, 20, 30], wishlist2 = [15, 25, 35]

Output:

[]

Note:

There are no common items between the two wishlists.

3

Example 3

Input:

wishlist1 = [1, 2, 2, 3, 4], wishlist2 = [2, 2, 4, 5, 6]

Output:

[2,4]

Note:

Items 2 and 4 are common. Despite multiple occurrences of 2 in both, it appears only once in the output. The result is sorted.

Constraints

Constraints & assumptions

  • 0 <= wishlist1.length, wishlist2.length <= 10^4
  • 1 <= item_id <= 10^9
  • item_id values are integers.
  • The solution should be reasonably efficient for the given constraints.

What interviewers evaluate

  • Correctness: Does the solution correctly identify all common items and handle duplicates/sorting as required?
  • Efficiency: Is the chosen algorithm efficient in terms of time and space complexity?
  • Clarity & Readability: Is the code well-structured, easy to understand, and maintainable?
  • Edge Cases: Does the solution handle edge cases like empty inputs or no common items gracefully?

Loading editor...