#437 Prefix Intersection Size of Two Lists

Companies:
VK
Themes:
AlgorithmsHash TablesPractice

Implement a function that calculates the sequence of intersection sizes for the prefixes of two identifier lists.

Algorithm description:

  • Two lists top1 and top2 of length N are given;
  • At step i, take prefixes of length i from both lists, treat them as sets, and find the cardinality (size) of their intersection;
  • Numbers in the lists may repeat, but only unique elements are considered when calculating the intersection;
  • The result is a list of N numbers, where each number is the size of the intersection of the corresponding prefixes.

The input lists are guaranteed to have equal lengths, so this does not need to be validated.

Examples:

Input: [1, 2, 3, 4], [3, 4, 5, 6]
Output: [0, 0, 1, 2]

Input: [1, 1, 1], [1, 1, 1]
Output: [1, 1, 1]

Input: [10, 7, 9], [1, 7, 3]
Output: [0, 1, 1]

Input: [3, 4, 5, 6], [1, 2, 3, 4]
Output: [0, 0, 1, 2]

Input: [1], [2]
Output: [0]

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in