KatInterview

Query-Driven Text Wrapper with Last-Line Balancing

StringsAuto-graded · 20 test cases
EasyAuto-gradedStringsstring-parsinggreedysimulation

Last updated

Parse a URL query string by hand, then greedily wrap the text to a width and fix a lone last-line word by borrowing from the previous line.

Query-Driven Text Wrapper

A rendering service receives a URL whose query string says which text to display and how wide each line may be. Write a function that reads these settings from the URL and returns the text broken into lines.

Step 1: Read the query string

  • The query string is everything after the first ? in the URL. If the URL has no ?, there are no parameters.
  • Parameters are separated by &. Skip empty pieces (for example, from &&).
  • Split each piece at its first = into a key and a value. Any further = characters belong to the value. A piece with no = has the whole piece as its key and an empty string as its value.
  • In each value (not in keys), replace every + and every %20 with a single space. No other percent-escapes are decoded, and no URL-parsing library may be used.
  • If the same key appears several times, the last occurrence wins.

Two parameters matter:

  • text: the string to wrap. If it is missing or empty, return [].
  • width: the maximum line length. Parse the (decoded) value as a base-10 integer, ignoring surrounding whitespace the way Python's int() does. If it is missing, not a valid integer, or not greater than zero, use 20.

Step 2: Greedy wrapping

  • Split text on whitespace into words. Runs of whitespace count as one separator. If this gives no words (the text was only whitespace), return [].
  • Build lines from left to right. Put each word on the current line if the line, with one space between words, would still have length at most width. Otherwise, close the current line and start a new one with that word.
  • Punctuation stuck to a word is part of that word.
  • Lines are the words joined by exactly one space.

Step 3: Last-line balancing

After wrapping, if there is more than one line and the last line holds exactly one word, try to move one word down to it:

  • Take the last word of the second-to-last line and put it in front of the lone last word.
  • Do this only if the second-to-last line had at least 2 words before the move, and the new last line (the moved word, a space, then the old last word) has length at most width.
  • Otherwise leave the lines unchanged.
  • Apply this adjustment at most once. Do not re-check earlier lines afterwards, even if the move leaves a one-word line behind.

Function

Implement wrap_query_text(url), which returns the list of lines.

Constraints

  • 1 <= len(url) <= 1000
  • 5 <= width <= 100 whenever a valid width is supplied
  • Every word is no longer than width
  • Do not use urllib.parse or any other URL-parsing library

Examples

Example 1

Input: wrap_query_text('https://airbnb.com/search?text=Cozy+cabin+in+the+woods&width=10')

Output: ['Cozy cabin', 'in', 'the woods']

Example 2

Input: wrap_query_text('https://airbnb.com/rooms?text=Quiet%20loft&width=20')

Output: ['Quiet loft']

Hints

Hint 1

Handle the two jobs separately: first turn the URL into a dictionary of parameters, then work only with the resulting text and width.

Hint 2

Split each parameter on the first '=' only (split('=', 1)). Decode '+' and '%20' in values only, and let later duplicate keys overwrite earlier ones. Remember the fallbacks: width defaults to 20, and missing, empty, or whitespace-only text gives [].

Hint 3

Pack words greedily while tracking the current line length (adding 1 for the space). After packing, run a single check on the last two lines for the lone-word case, and only move the word if the new last line still fits in width.