Quick Overview

This question evaluates understanding of sorting algorithms, stability, merging strategies, and time/space complexity analysis when ordering strings by length, and is categorized under Coding & Algorithms.

Sort and merge string lists by length

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given two unsorted lists of strings, return a single list sorted by ascending string length. Specify your tie-breaking rule for equal-length strings (e.g., preserve original relative order for stability). Analyze time and space complexity and whether your sort is stable. Follow-up: If each input list is already individually sorted by length with the same tie-breaker, design and implement an efficient merge to produce a globally length-sorted list. Discuss in-place versus extra-memory approaches and their complexities.

Overview: This question evaluates understanding of sorting algorithms, stability, merging strategies, and time/space complexity analysis when ordering strings by length, and is categorized under Coding & Algorithms.

Return all strings sorted by ascending length. Ties preserve original relative order, treating list1 followed by list2 as the original order. If already_sorted=True, assume each input is already length-sorted with the same tie rule and merge them linearly.

Constraints

  • Input strings may have equal lengths
  • When already_sorted=True both lists are individually sorted by length

Examples

Input: (['pear', 'a', 'plum'], ['bb', 'apple'], False)

Expected Output: ['a', 'bb', 'pear', 'plum', 'apple']

Input: (['a', 'bb', 'cccc'], ['d', 'eee'], True)

Expected Output: ['a', 'd', 'bb', 'eee', 'cccc']

Hints

  1. Stable sorting solves the unsorted case.
  2. The follow-up is the merge step from merge sort.

Sort and Merge String Lists by Length

mediumAppleSoftware Engineer

Return all strings sorted by ascending length. Ties preserve original relative order, treating list1 followed by list2 as the original order. If already_sorted=True, assume each input is already length-sorted with the same tie rule and merge them linearly.

Examples

Example 1
Input
(['pear', 'a', 'plum'], ['bb', 'apple'], False)
Output
['a', 'bb', 'pear', 'plum', 'apple']
Example 2
Input
(['a', 'bb', 'cccc'], ['d', 'eee'], True)
Output
['a', 'd', 'bb', 'eee', 'cccc']

Constraints

  • •Input strings may have equal lengths
  • •When already_sorted=True both lists are individually sorted by length

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Case 1
Input
(['pear', 'a', 'plum'], ['bb', 'apple'], False)
Expected
['a', 'bb', 'pear', 'plum', 'apple']
Case 2
Input
(['a', 'bb', 'cccc'], ['d', 'eee'], True)
Expected
['a', 'd', 'bb', 'eee', 'cccc']

Browse More Questions