PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's competence in grid traversal, distance computation using Manhattan metrics, and handling tie-breaking rules including lexicographic ordering.

  • medium
  • Uber
  • Coding & Algorithms
  • Backend Engineer

Replace Dashes With Nearest Letters

Company: Uber

Role: Backend Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given an `m x n` grid containing only dash characters `'-'` and alphabetic letters. For every dash cell, replace it with the nearest letter in the grid. Distance is Manhattan distance using 4-directional movement: up, down, left, and right. Letter cells remain unchanged. If a dash cell has multiple nearest letters at the same minimum distance, any one of those letters may be used. **Follow-up:** Change the tie-breaking rule so that if a dash cell has multiple nearest letters at the same minimum distance, it must be replaced by the lexicographically smallest of those letters. Return the modified grid.

Quick Answer: This question evaluates a candidate's competence in grid traversal, distance computation using Manhattan metrics, and handling tie-breaking rules including lexicographic ordering.

Part 1: Replace Dashes with Any Nearest Letter

You are given a rectangular grid of lowercase English letters and '-' characters. For every dash cell, replace it with a nearest letter from the grid. Distance is Manhattan distance using 4-directional movement: up, down, left, and right. Existing letter cells must remain unchanged. If a dash cell has multiple nearest letters at the same minimum distance, any one of them is acceptable. Return the completed grid. For the sample tests below, tie cases are avoided so the expected output is unique.

Constraints

  • 0 <= m <= 200, where m is the number of rows
  • If m > 0, then 1 <= n <= 200, where n is the number of columns
  • All rows have the same length
  • Each cell is either '-' or a lowercase English letter
  • If the grid is non-empty, it contains at least one letter

Examples

Input: []

Expected Output: []

Explanation: Edge case: an empty grid stays empty.

Input: ['q']

Expected Output: ['q']

Explanation: Edge case: a single letter cell is unchanged.

Input: ['ab-', '---']

Expected Output: ['abb', 'abb']

Explanation: Each dash is uniquely closer to either 'a' or 'b'.

Input: ['m---', '----']

Expected Output: ['mmmm', 'mmmm']

Explanation: With only one letter in the grid, every dash becomes that letter.

Input: ['abc', 'def']

Expected Output: ['abc', 'def']

Explanation: There are no dashes to replace.

Hints

  1. Instead of running a search from every dash, start from all letter cells at once.
  2. A multi-source BFS spreads outward in increasing Manhattan distance, so the first time a dash is reached, it has found a nearest letter.

Part 2: Replace Dashes with the Lexicographically Smallest Nearest Letter

You are given a rectangular grid of lowercase English letters and '-' characters. For every dash cell, replace it with the nearest letter in the grid. Distance is Manhattan distance using 4-directional movement: up, down, left, and right. Existing letter cells must remain unchanged. If a dash cell has multiple nearest letters at the same minimum distance, you must choose the lexicographically smallest of those letters. Return the completed grid.

Constraints

  • 0 <= m <= 200, where m is the number of rows
  • If m > 0, then 1 <= n <= 200, where n is the number of columns
  • All rows have the same length
  • Each cell is either '-' or a lowercase English letter
  • If the grid is non-empty, it contains at least one letter

Examples

Input: []

Expected Output: []

Explanation: Edge case: an empty grid stays empty.

Input: ['z-a']

Expected Output: ['zaa']

Explanation: The middle cell is distance 1 from both 'z' and 'a', so it becomes 'a'.

Input: ['a--', '---', '--b']

Expected Output: ['aaa', 'aab', 'abb']

Explanation: Cells tied between 'a' and 'b' must choose 'a', the smaller letter.

Input: ['-', 'c']

Expected Output: ['c', 'c']

Explanation: Edge case: a single-column grid with one dash above a letter.

Input: ['abc', 'def']

Expected Output: ['abc', 'def']

Explanation: There are no dashes, so the grid is unchanged.

Hints

  1. You still want a multi-source BFS, because all edges have equal weight.
  2. To enforce lexicographic tie-breaking, initialize the BFS frontier so smaller letters expand first among cells at the same distance.
Last updated: May 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Count Islands After Land Additions - Uber (medium)
  • Implement Last-Click Attribution APIs - Uber (medium)