KatInterview

Three-Part Coding Challenge: Justification, Wildcards, and Pathfinding

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingstringsgraphsbfs

Last updated

Evaluates text formatting with precise spacing rules, wildcard-based string validation, and grid traversal with path reconstruction.

Solve three independent algorithmic problems and return your answers in a single dictionary.

Part 1: Full-Width Text Justification

You receive a list of words and a maximum line width. Arrange the words into lines where every line has exactly max_width characters and follows full justification rules.

Line packing:

  • Fit as many words as possible on each line using a greedy approach
  • To determine if word at index j fits on a line starting at index i, check that the sum of all word lengths plus the minimum required spaces between them does not exceed max_width
  • Specifically, words from index i to j (inclusive) fit if: sum_of_word_lengths + (j - i) <= max_width, where (j - i) represents the mandatory single space before each word after the first

Line formatting:

  • Last line of the output: Left-justify with single spaces between words and pad remaining space on the right
  • Lines containing only one word: Left-justify (no gaps between words exist) and pad remaining space on the right
  • All other lines: Distribute spaces between words as evenly as possible. If spaces cannot be divided evenly among gaps, assign extra spaces to the leftmost gaps first. Specifically, if there are g gaps and s total spaces, each gap gets s // g spaces, and the first s % g gaps each get one additional space

Each output line must be exactly max_width characters long.

Guaranteed:

  • Each word's length is at most max_width
  • At least one word exists

Return a list of formatted strings.

Part 2: Wildcard Parenthesis Validation

Given a string containing only '(', ')', and '*' characters, determine if there exists any way to interpret the wildcards such that the result is a valid balanced parenthesis sequence.

Each '*' can be interpreted as:

  • An opening parenthesis '(', or
  • A closing parenthesis ')', or
  • An empty string (effectively removed)

A balanced parenthesis sequence means:

  • Every opening parenthesis has a matching closing parenthesis
  • At no point while reading left-to-right does the count of closing parentheses exceed opening parentheses
  • The final count of opening and closing parentheses is equal

Return True if at least one valid interpretation exists, otherwise False.

Part 3: Shortest Grid Path

Given a 2D grid where 0 represents an open cell and 1 represents an obstacle, find any shortest path from a start coordinate to a target coordinate.

Movement rules:

  • You can move up, down, left, or right (four directions)
  • Each move has unit cost
  • You cannot move through obstacles (cells with value 1)
  • You cannot move outside the grid boundaries

Input coordinates:

  • start: [row, col] representing the starting position
  • target: [row, col] representing the target position

Return value:

  • If a path exists: return one shortest path as a list of [row, col] coordinates from start to target (both inclusive)
  • If no path exists: return an empty list []
  • If start or target is out of bounds: return []
  • If start or target is on an obstacle: return []
  • If start equals target and the cell is valid: return [[start_row, start_col]]

When exploring neighbors during pathfinding, check them in this order: up (-1, 0), down (1, 0), left (0, -1), right (0, 1).

Function Signature

Your function receives six parameters:

  • words: list of strings
  • max_width: positive integer
  • s: string containing only '(', ')', and '*'
  • grid: 2D list of integers (each cell is 0 or 1)
  • start: list of two integers [row, col]
  • target: list of two integers [row, col]

Return a dictionary with exactly three keys:

  • "justified_lines": list of strings (Part 1 answer)
  • "valid_parentheses": boolean (Part 2 answer)
  • "shortest_path": list of [row, col] pairs (Part 3 answer)

Constraints

  • 0 ≤ len(words) ≤ 300
  • 1 ≤ len(word) ≤ max_width ≤ 100 for each word
  • 1 ≤ max_width ≤ 100
  • 0 ≤ len(s) ≤ 1000
  • 0 ≤ len(grid) ≤ 100
  • 0 ≤ len(grid[0]) ≤ 100 (if grid is non-empty)

Examples

Example 1

Input: solve_algorithm_trio(['This', 'is', 'an', 'example', 'of', 'text', 'justification.'], 16, '(*)', [[0, 0, 0], [1, 0, 0], [0, 0, 0]], [0, 0], [2, 2])

Output: {'justified_lines': ['This is an', 'example of text', 'justification. '], 'valid_parentheses': True, 'shortest_path': [[0, 0], [0, 1], [1, 1], [2, 1], [2, 2]]}

Example 2

Input: solve_algorithm_trio(['What', 'must', 'be', 'acknowledgment', 'shall', 'be'], 16, '((*)', [[0, 1], [0, 0]], [0, 0], [1, 1])

Output: {'justified_lines': ['What must be', 'acknowledgment ', 'shall be '], 'valid_parentheses': True, 'shortest_path': [[0, 0], [1, 0], [1, 1]]}

Hints

Hint 1

For text justification, track the cumulative character count as you add words to a line. Remember that each word after the first requires at least one space before it.

Hint 2

For parenthesis validation, maintain a range of possible balance values rather than a single balance. Wildcards expand this range.

Hint 3

For grid pathfinding, BFS with unit costs guarantees the first time you reach a cell is via a shortest path. Store parent pointers to reconstruct the path afterward.