Katkat

Solve Two OA Coding Problems

SalesforceSoftware Engineer · Online Assessment

Last 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:

  1. 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 returns 5.
  1. Minimum operations to reduce an integer to zero
  • You are given a positive integer n.
  • In one operation:
  • if n is even, you may replace it with n / 2;
  • if n is odd, you may replace it with either n + 1 or n - 1.
  • Compute the minimum number of operations required to reduce n to 0.
  • Example: if n = 7, one optimal sequence is 7 -> 8 -> 4 -> 2 -> 1 -> 0, which takes 5 operations.