Question presented to candidate: "Two non-negative integers are stored as linked lists, one digit per node, ones digit first: 342 is 2 โ 4 โ 3. Return their sum as a linked list in the same format. Please write it in Python."
What a strong answer should cover:
- Column addition: walk both lists together,
total = carry + x + y, appendtotal % 10, carrytotal // 10. - Loop while either list or the carry remains, treating a missing digit as 0; a dummy head avoids a special case for the first node.
- O(max(m, n)) time, output of at most max(m, n) + 1 nodes.
- The forward-order follow-up: stacks (or reversing) plus prepending.
- Python specifics:
divmod(total, 10); Python ints never overflow, so "convert, add, convert back" does work for moderate sizes, butint(str)refuses inputs over 4300 digits by default and the interviewer is testing list handling anyway.
Clarifying questions expected:
- "Ones digit first? Can the lists have different lengths?"
- "May I modify the inputs, and how long can the numbers be?"
Code / implementation expected: Yes โ add_two_numbers(l1, l2) in Python, building a new list.