Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array Common Elements

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

Your question is Array Common Elements. 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

A Cloudera Data Platform processing component receives two batches of integer identifiers and needs to determine which identifiers appear in both batches. Return the unique common elements, preserving the order in which they first appear in nums1.

Implement a function that does not modify either input array.

Formal Specification

Given two arrays of integers, nums1 and nums2, return an array containing every distinct value that occurs at least once in both arrays. The result must follow nums1's first-occurrence order. If there are no common values, return an empty array.

Constraints

  • 0 <= len(nums1), len(nums2) <= 10^5
  • -10^9 <= nums1[i], nums2[i] <= 10^9
  • Duplicate values may appear in either array.
  • The output must contain unique values in nums1 first-occurrence order.

Function Signature

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