Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Intersection of Two Arrays

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

Your question is Intersection of Two Arrays. 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 UC Santa Barbara course-planning tool receives two arrays of course codes from different search results. Return the distinct course codes that appear in both arrays, preserving the order of their first appearance in codes1.

Formal Specification

Implement intersection(codes1, codes2), where both inputs are arrays of integers representing course codes. Return an array containing each shared value exactly once. If a value occurs multiple times in either input, it must appear at most once in the output. The output order must follow the first occurrence of each value in codes1.

Constraints

  • 0 <= len(codes1), len(codes2) <= 10^5
  • -10^9 <= codes1[i], codes2[i] <= 10^9
  • Each shared value must appear at most once in the output
  • Output order follows first appearance in codes1

Function Signature

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