Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Intersection With Duplicates

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Intersection With Duplicates. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Meta IT's mobile diagnostics workflows may compare two lists of event identifiers and need to retain repeated identifiers. Given two integer arrays, return their intersection, preserving duplicates and the order in which values appear in the first array.

A value must appear in the result as many times as it appears in both arrays. The result should include no additional values and may be returned in any order only if the ordering rule is explicitly followed: here, preserve the relative order from nums1.

Formal Specification

Implement intersect_arrays(nums1, nums2).

  • Input: Two lists of integers, nums1 and nums2.
  • Output: A list containing each common value min(count(nums1, value), count(nums2, value)) times, ordered by its occurrences in nums1.
  • The input arrays must not be modified.

Constraints

  • 0 <= len(nums1), len(nums2) <= 10^5
  • -10^9 <= nums1[i], nums2[i] <= 10^9
  • The input arrays must not be modified
  • The output preserves the relative order from nums1

Function Signature

def intersect_arrays(nums1, nums2):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output