#404 Count of Common Elements in Array Prefixes

Companies:
Yandex
Themes:
AlgorithmsHash TablesPractice

Two arrays containing numbers from 1 to N are given. For each of the N indexes, find the number of unique elements in the intersection of the sets formed by the first and second array prefixes.

Example:
1 2 3 5 4
5 3 7 4 1

Answer:
0 0 1 2 3

Here:

  • At index zero there are no common elements among the currently available values: 1 in the first array and 5 in the second.
  • At index one there are also none: 1,2 in the first array and 5,3 in the second.
  • At index two: 1,2,3 in the first array and 5,3,7 in the second. The intersection contains 3, so write 1.

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