PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates proficiency in string manipulation, run-length encoding concepts, parsing numeric counts, and validation of encoded formats, including handling invalid inputs. It is in the Coding & Algorithms domain and is primarily a practical implementation task that also probes conceptual understanding of parsing, edge-case reasoning, and efficiency trade-offs.

  • medium
  • NVIDIA
  • Coding & Algorithms
  • Software Engineer

Implement string compression and decompression

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement two functions for a simple string compression format. - `compress(s)`: convert a string into groups of consecutive equal characters written as `<char><count>`. - `decompress(t)`: reconstruct the original string from that encoded form. Example: - `compress("aaabbccccd")` -> `"a3b2c4d1"` - `decompress("a3b2c4d1")` -> `"aaabbccccd"` Assume the input contains only English letters. Counts may have multiple digits, such as `a12`. Explain how you would handle invalid encoded input.

Quick Answer: This question evaluates proficiency in string manipulation, run-length encoding concepts, parsing numeric counts, and validation of encoded formats, including handling invalid inputs. It is in the Coding & Algorithms domain and is primarily a practical implementation task that also probes conceptual understanding of parsing, edge-case reasoning, and efficiency trade-offs.

Implement a function `solution(mode, text)` that supports two operations for a simple run-length encoding format. - If `mode == "compress"`, convert the input string into groups of consecutive equal letters written as `<char><count>`. - If `mode == "decompress"`, rebuild the original string from that encoded form. Examples: - `solution("compress", "aaabbccccd")` -> `"a3b2c4d1"` - `solution("decompress", "a3b2c4d1")` -> `"aaabbccccd"` Rules: - Input for compression contains only English letters (`A-Z` and `a-z`). - Counts in the encoded string may have multiple digits, such as `a12`. - For decompression, treat the encoded string as invalid unless it is a sequence of: `letter`, then a positive integer count, repeated. - Invalid encoded input should return `None`. - A count of `0` or any count with leading zeros (such as `a03`) is invalid. - The empty string is valid for both operations and should return the empty string.

Constraints

  • 0 <= len(text) <= 100000
  • For `compress`, `text` contains only English letters `A-Z` and `a-z`
  • For `decompress`, a valid encoding alternates `letter` + `positive integer count`
  • For valid decompression inputs, the total decompressed output length will not exceed 100000

Examples

Input: ("compress", "aaabbccccd")

Expected Output: "a3b2c4d1"

Explanation: The runs are `a x 3`, `b x 2`, `c x 4`, and `d x 1`.

Input: ("decompress", "a3b2c4d1")

Expected Output: "aaabbccccd"

Explanation: Expand each letter by its count: `a3 -> aaa`, `b2 -> bb`, `c4 -> cccc`, `d1 -> d`.

Input: ("compress", "")

Expected Output: ""

Explanation: An empty string compresses to an empty string.

Input: ("decompress", "a12B3")

Expected Output: "aaaaaaaaaaaaBBB"

Explanation: `a12` expands to 12 lowercase `a` characters and `B3` expands to 3 uppercase `B` characters.

Input: ("decompress", "ab2")

Expected Output: None

Explanation: Invalid: after `a`, a numeric count is required, but `b` appears immediately.

Input: ("decompress", "a03")

Expected Output: None

Explanation: Invalid: counts cannot start with `0`, so leading zeros are not allowed.

Hints

  1. For compression, scan left to right and count the length of each run of identical characters.
  2. For decompression, after reading one letter, keep reading digits until the next letter to build the full count. Make sure the count exists and does not start with `0`.
Last updated: May 12, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,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

  • Return all file paths via DFS - NVIDIA (easy)
  • Implement a disk space manager with eviction - NVIDIA (medium)
  • Implement short algorithms on logs, grids, and strings - NVIDIA (hard)
  • Implement encode/decode for list of strings - NVIDIA (easy)
  • Solve small string and API tasks - NVIDIA (medium)