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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
Addition of Two Linked Lists
00:00
5 left

Addition of Two Linked Lists

EasyPython

Problem

ThoughtSpot can represent large numeric values as digit streams when the values exceed native integer limits. Given two non-empty linked lists, add the represented non-negative integers and return the sum as a linked list.

Each node contains one decimal digit, and the least significant digit appears first. The result must also use reverse digit order. Do not convert either linked list into a built-in integer or string.

Formal Specification

Use a ListNode with fields val and next. The function add_two_numbers(l1, l2) receives the heads of two linked lists and returns the head of a newly created linked list representing their sum. The input lists may have different lengths. Input values do not contain leading zeroes unless the number itself is zero.

For test cases, lists are shown as arrays for readability. The harness converts each array to a linked list before calling the function and serializes the returned list back to an array.

Constraints

  • 1 <= len(l1), len(l2) <= 100,000
  • 0 <= node.val <= 9
  • Both input lists are valid and non-empty
  • The output may contain one additional node for a final carry
  • Do not convert the linked lists to integers or strings

Function Signature

def add_two_numbers(l1, l2):
Interviewer

Your question is Addition of Two Linked 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.