Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Common Elements in Sorted Lists

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

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

Criteo's advertising systems compare sorted identifier sequences during audience and event-processing workflows. Given two sorted integer lists, return their common values in sorted order.

Formal Specification

Implement intersect_sorted_lists(first, second). The inputs are lists of integers sorted in nondecreasing order. Return a new list containing each common integer as many times as it appears in both inputs. In other words, the output contains the minimum frequency from the two input lists. Do not modify either input list.

Use a two-pointer approach that takes advantage of the sorted inputs.

Constraints

  • 0 <= len(first), len(second) <= 10^5
  • -10^9 <= first[i], second[j] <= 10^9
  • Both inputs are sorted in nondecreasing order
  • Duplicate values may occur
  • The inputs must not be modified

Function Signature

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