Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Common Elements in Two Arrays

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

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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output