← Salesforce Interview Insights

Salesforce·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Salesforce SWE interview with an in-place string compression problem. Pretty standard algorithmic round, nothing flashy, but the constant space constraint is where people trip up.

Questions Asked (1)

Q1

Given an array of characters, compress it in-place by replacing consecutive repeated characters with the character followed by its count. Group lengths of 10 or more should be split into individual digit characters. Return the new length of the array using only constant extra space.

Algorithms & Data Structures
Author's notes

The basic logic clicks pretty fast: iterate, count runs, write back.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique: one pointer to read through the array and another to write the compressed result in-place. For each group of consecutive identical characters, write the character followed by the digits of its count, handling counts of 10 or more by writing each digit separately. Finally, return the write pointer as the new length.

Pro tip: Clarify that the array may have extra space beyond the new length, and that only the first 'new length' characters matter. Also, mention that the algorithm runs in O(n) time and O(1) space, which is optimal.

1. Initialize pointers

Set a read pointer (i) to 0 and a write pointer (write) to 0. The read pointer will scan the original array, and the write pointer will track the position for the next compressed character.

2. Iterate through groups

While i < length of array, identify the current character and count how many times it repeats consecutively by advancing a second pointer (j) until the character changes.

3. Write compressed data

Write the current character at the write pointer and increment it. If the count is greater than 1, convert the count to a string and write each digit individually at the write pointer, incrementing it for each digit.

4. Update read pointer

Set i = j to move to the next group of characters.

5. Return new length

After the loop, return the write pointer as the new length of the compressed array.

Key Points to Mention

  • Two-pointer technique for in-place modification
  • Handling counts of 10 or more by splitting into digits
  • Constant extra space (O(1) space complexity)
  • Time complexity O(n) where n is the length of the array
  • Edge cases: empty array, single character, all characters same
  • In-place means modifying the input array directly without using additional data structures

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.