Merge Two Sorted Roster Files

Hard ⏱ 18 min 33% acceptance ★★★★☆ 4.3
Two files each contain names, one per line, already sorted alphabetically. Write merge_rosters(path_a, path_b, dest) producing a single sorted file. For extra credit do a true two-pointer merge, but reading both and sorting is acceptable if memory is no concern — implement the two-pointer version here.

Examples

Example 1
Input
merge_rosters('a.txt', 'b.txt', 'all.txt')  # a: ann,zoe  b: bob
Output
all.txt: ann, bob, zoe
Explanation

Classic merge step of merge sort, on files.

Constraints

  • Two-pointer merge — no sorting the combined list.
  • Preserve duplicates from both files.

Topics

File Handlingmerging

Companies

AmazonGoldman Sachs

Hints

Hint 1

Read both into lists of stripped lines first.

Hint 2

Advance the pointer whose current name is smaller.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.