← Salesforce Interview Insights

Salesforce·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jun 2026

Summary

Salesforce OA for a software engineer role, two coding problems. Nothing too wild but the second one had me second-guessing my approach the whole time.

Questions Asked (2)

Q1

Given an array of characters, compress consecutive runs of the same character in place. Write the character once, then the count as individual digit characters if the run is longer than one. Return the new length.

Algorithms & Data Structures
Author's notes

Two pointers ended up being the move here.

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 identify runs, and another to write the compressed result in place. For each run, write the character and then the digits of the count (if >1) as separate characters. Return the write pointer's final position as the new length.

Pro tip: Clarify that the input array is mutable and that we must modify it in place with O(1) extra space. Also, handle edge cases like empty array and runs of length 1, and mention that the count can be multi-digit, so you need to convert the integer to individual digit characters.

1. Clarify requirements and edge cases

Confirm that the array is mutable, compression is in place, and return the new length. Discuss edge cases: empty array, single character, runs longer than 9 (multi-digit counts).

2. Initialize pointers

Set a read pointer to scan the array and a write pointer to overwrite with compressed data. Start both at index 0.

3. Iterate and count runs

While read < length, record the current character, then advance read while the same character repeats, counting the run length.

4. Write compressed data

Write the character at the write pointer, increment write. If count > 1, convert count to string and write each digit character, incrementing write for each.

5. Return new length

After processing all runs, return the write pointer as the new length of the compressed array.

Key Points to Mention

  • In-place modification with O(1) extra space
  • Two-pointer technique (read and write pointers)
  • Handling multi-digit counts by writing each digit separately
  • Time complexity O(n) and space complexity O(1)
  • Edge cases: empty array, single character, runs of length 1
  • Returning the new length, not the modified array

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

Q2

Given a positive integer n, find the minimum number of operations to reduce it to zero. If n is even, divide by 2. If n is odd, either add 1 or subtract 1.

Algorithms & Data Structures
Author's notes

Looks like a simple greedy but I kept doubting myself.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose a greedy strategy based on the binary representation of n. Explain that for odd n, the optimal choice depends on n mod 4, and prove why this greedy approach yields the minimum number of operations.

Pro tip: Mention that this problem is equivalent to finding the shortest path in a graph where each number connects to n/2, n+1, or n-1, and that the greedy solution is optimal due to the structure of binary numbers. This shows depth and can lead to a discussion on BFS vs greedy trade-offs.

1. Clarify the problem

Restate the problem in your own words and confirm the rules: for even n, only divide by 2; for odd n, you can add or subtract 1. Ask about constraints (e.g., n up to 2^31) and whether n=0 is possible.

2. Explore small examples

Manually compute the minimum operations for small n (e.g., 1, 2, 3, 4, 5, 6, 7, 8) to identify patterns. Notice that for odd n, the choice depends on n mod 4.

3. Derive the greedy strategy

For even n, always divide by 2. For odd n > 1, if n % 4 == 1, subtract 1; if n % 4 == 3, add 1 (except when n == 3, subtract 1). For n == 1, subtract 1 to reach 0.

4. Prove optimality

Argue that the greedy choice is optimal by considering the binary representation: adding 1 to an odd number ending in '11' causes a carry that reduces the number of 1s, while subtracting 1 from an odd number ending in '01' does the same. This minimizes future operations.

5. Analyze complexity and implement

The algorithm runs in O(log n) time and O(1) space. Provide pseudocode or code, and test with edge cases like n=1, n=3, and large powers of 2.

Key Points to Mention

  • Greedy approach based on n mod 4 for odd numbers
  • Binary representation and bit manipulation insight
  • Time complexity O(log n) and space O(1)
  • Edge cases: n=1, n=3, and powers of 2
  • Proof of optimality using binary carry logic
  • Alternative BFS approach and why greedy is better

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