The core problem wasn't too bad once I realized I needed two pointers, one for reading and one for writing.
Use a two-pointer approach: one pointer reads through the array to identify runs, and another writes the compressed result in-place. For each run, write the character, then if the run length is greater than 1, write each digit of the count as a separate character. Return the write pointer as the new length.
Pro tip: Clarify with the interviewer whether the input array is mutable and whether the compressed result must overwrite the original array from the beginning. Also, discuss edge cases like empty array, single character, and runs longer than 9 to show thoroughness.
Restate the problem to ensure clarity: compress runs in-place, O(1) extra space, and return new length. Ask clarifying questions about input mutability and expected output format.
Initialize a write pointer at 0 and a read pointer at 0. Iterate through the array to find the end of each run, then write the character and its count (if >1) at the write pointer, advancing it accordingly.
For run lengths >1, convert the integer count to a string and write each digit as a character. Consider edge cases: empty array, runs of length 1, and runs longer than 9 (multi-digit counts).
Write clean code, then walk through examples like ['a','a','b','b','c','c','c'] to verify correctness. Ensure the write pointer never overtakes the read pointer to avoid overwriting unprocessed data.
State that time complexity is O(n) and space complexity is O(1). Discuss potential trade-offs, such as readability vs. in-place efficiency, and mention alternative approaches if extra space were allowed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.