Solve Two OA Coding Problems
SalesforceSoftware Engineer · Online AssessmentLast updated
These two problems evaluate in-place array manipulation and run-length encoding implementation for character sequences, and algorithmic reasoning about integer operations and minimal-operation sequences, falling squarely within the Coding & Algorithms domain.
The online assessment included two coding problems:
- In-place character run compression
- You are given an array of characters.
- Compress consecutive identical characters in place.
- For each maximal run, write the character once, followed by the decimal count only if the run length is greater than 1.
- The count must be written as individual digit characters.
- Return the new length of the compressed array.
- Example:
['a','a','a','b','b','c']becomes['a','3','b','2','c'], and the function returns5.
- Minimum operations to reduce an integer to zero
- You are given a positive integer
n. - In one operation:
- if
nis even, you may replace it withn / 2; - if
nis odd, you may replace it with eithern + 1orn - 1. - Compute the minimum number of operations required to reduce
nto0. - Example: if
n = 7, one optimal sequence is7 -> 8 -> 4 -> 2 -> 1 -> 0, which takes5operations.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.