Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Common Elements in Two Arrays
00:00
5 left

Common Elements in Two Arrays

EasyPython

Problem

While processing value lists from Meesho Catalog workflows, find the values that appear in both input arrays. Return each common value only once, arranged in ascending numeric order.

Formal Specification

Implement intersection_sorted(arr1, arr2).

  • Input: Two arrays of integers, arr1 and arr2.
  • Output: An array containing the distinct integers present in both inputs, sorted in nondecreasing order.
  • The input arrays may be unsorted and may contain duplicate values.
  • Return an empty array when the arrays have no common values.

Examples

Example 1

Input: arr1 = [5, 2, 2, 8, 1], arr2 = [2, 3, 5, 5]
Output: [2, 5]

The values 2 and 5 occur in both arrays. Duplicates are removed, and the result is sorted.

Example 2

Input: arr1 = [7, 4, 9], arr2 = [1, 2, 3]
Output: []

The arrays have no common values.

Constraints

  • 0 <= len(arr1), len(arr2) <= 10^5
  • -10^9 <= arr1[i], arr2[i] <= 10^9
  • The input arrays may contain duplicate integers.
  • Do not modify either input array.

Constraints

  • 0 <= len(arr1), len(arr2) <= 10^5
  • -10^9 <= arr1[i], arr2[i] <= 10^9
  • The input arrays may contain duplicate integers
  • Do not modify either input array

Function Signature

def intersection_sorted(arr1, arr2):
Interviewer

Your question is Common Elements in Two Arrays. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.