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 From Two Lists
00:00
5 left

Common Elements From Two Lists

EasyPython

Problem

Exiger DDI may compare supplier or entity identifiers from two screening results. Given two lists of values, return the unique values that occur in both lists, preserving the order in which they first appear in the first list.

Formal Specification

Implement common_elements(list1, list2):

  1. list1 and list2 are lists of integers.
  2. Return a new list containing each shared value exactly once.
  3. Preserve the order from list1.
  4. Do not modify either input list.
  5. Return an empty list when the lists have no common values.

Examples

Example 1

Input: list1 = [4, 2, 7, 2, 9], list2 = [8, 2, 4, 2]

Output: [4, 2]

Explanation: 4 and 2 occur in both lists. The result follows their first appearance in list1, and the duplicate 2 is returned once.

Example 2

Input: list1 = [5, 1, 3], list2 = [7, 8]

Output: []

Explanation: The lists contain no shared values.

Constraints

  • 0 <= len(list1), len(list2) <= 100,000
  • -10^9 <= list1[i], list2[i] <= 10^9
  • Inputs may contain duplicates.
  • The output must contain no duplicates.

Constraints

  • 0 <= len(list1), len(list2) <= 100,000
  • -10^9 <= list1[i], list2[i] <= 10^9
  • Inputs may contain duplicate values
  • The output must contain each common value at most once
  • Neither input list may be modified

Function Signature

def common_elements(list1, list2):
Interviewer

Your question is Common Elements From Two Lists. 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.