← Questions

Compare two linked lists of string chunks

0
MetaCodingLast asked Jul 2025

A coding interview question asked in Meta's technical phone screen, last asked Jul 2025. It tests linked lists, strings and two pointers.

Two singly linked lists each spell out a piece of text, one string per node. Return whether both lists spell exactly the same text. Start with the simple version, where every node holds one character, then handle nodes whose strings have different lengths, without building either full string.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • ListNode has val: str and next: ListNode | None.
  • same_text(a: ListNode | None, b: ListNode | None) -> bool — True when the concatenation of all vals in a equals that of b. Comparison is case-sensitive.

Example

one character per node:
"a" -> "b" -> "c"   vs   "a" -> "b" -> "c"        ->  True
"a" -> "b"          vs   "a" -> "b" -> "c"        ->  False   # one list ends first

chunks of any length:
"he" -> "llo"       vs   "h" -> "el" -> "lo"      ->  True    # both spell "hello"
"ab" -> ""  -> "c"  vs   "abc"                    ->  True    # empty nodes are allowed
"ab" -> "d"         vs   "a" -> "bc"              ->  False

Constraints

  • Up to 10^5 nodes per list; total text length up to 10^6.
  • Target O(total length) time and O(1) extra space.

Clarifying questions

  • Does every node hold exactly one character, or can lengths vary? Can a node hold an empty string?
  • Is the comparison case-sensitive?
  • Can either list be empty?
  • Am I allowed to concatenate into strings, or is memory the point?

What a strong answer covers

  • One character per node: walk both lists in step, compare values, and check both end together.
  • Variable chunks: keep a pointer (node, offset) into each list, compare character by character, and move to the next node when an offset reaches the end of its string, skipping empty nodes.
  • Why building both strings costs O(total length) memory, which defeats the point.
  • Tests: both empty, one empty, empty nodes, a mismatch in the last character, lists of different total length.

Follow-ups

  • Compare the texts ignoring case and spaces.
  • Return the first position where the texts differ.
  • Compare lexicographically (return -1, 0 or 1) instead of only for equality.

Where it's asked

  • Meta · Technical phone screen · Last asked Jul 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

linked lists · strings · two pointers

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding