PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Solve Two OA Coding Problems

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

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`. 2. **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.

Quick Answer: 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.

Part 1: In-Place Character Run Compression

Given a list `chars` where each element is a one-character string, compress consecutive identical characters in place. For each maximal run, write the character once, followed by the run length only if the length is greater than 1. The count must be written as separate digit characters. Return the new length of the compressed list. For example, `['a','a','a','b','b','c']` becomes `['a','3','b','2','c']`, and the function returns `5`.

Constraints

  • 0 <= len(chars) <= 100000
  • Each element of `chars` is a string of length 1
  • The algorithm should run in linear time
  • Only constant extra auxiliary space should be used

Examples

Input: (['a','a','a','b','b','c'],)

Expected Output: 5

Explanation: The list is compressed to `['a','3','b','2','c']`, so the new length is 5.

Input: (['a'],)

Expected Output: 1

Explanation: A single character stays unchanged.

Input: ([],)

Expected Output: 0

Explanation: An empty list remains empty.

Input: (['a','b','c'],)

Expected Output: 3

Explanation: There are no repeated consecutive characters, so the compressed form is the same.

Input: (['x','x','x','x','x','x','x','x','x','x','x','x'],)

Expected Output: 3

Explanation: Twelve `x` characters compress to `['x','1','2']`.

Hints

  1. Use one pointer to scan runs of equal characters and another pointer to write the compressed result.
  2. When a run length is greater than 1, convert the count to a string and write each digit separately.

Part 2: Minimum Operations to Reduce an Integer to Zero

Given a positive integer `n`, compute the minimum number of operations needed to reduce it to `0`. 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`.

Constraints

  • 1 <= n <= 2147483647
  • If `n` is even, the next value must be `n // 2`
  • If `n` is odd, you may choose either `n + 1` or `n - 1`
  • The solution should be efficient for large values of `n`

Examples

Input: (7,)

Expected Output: 5

Explanation: One optimal sequence is `7 -> 8 -> 4 -> 2 -> 1 -> 0`.

Input: (1,)

Expected Output: 1

Explanation: The only move is `1 -> 0`.

Input: (8,)

Expected Output: 4

Explanation: The sequence is `8 -> 4 -> 2 -> 1 -> 0`.

Input: (3,)

Expected Output: 3

Explanation: The optimal sequence is `3 -> 2 -> 1 -> 0`.

Input: (15,)

Expected Output: 6

Explanation: An optimal path is `15 -> 16 -> 8 -> 4 -> 2 -> 1 -> 0`.

Hints

  1. For an odd number, both choices make it even. Usually it is better to pick the one that creates more trailing zeros in binary.
  2. There is one important special case: when `n == 3`, subtracting 1 is better than adding 1.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Maximize events attended given date ranges - Salesforce (medium)
  • Implement common data-structure and JS tasks - Salesforce (medium)
  • Minimize operations to reduce integer to zero - Salesforce (medium)
  • Implement an LFU cache with O(1) operations - Salesforce (medium)
  • Flatten a nested JSON object - Salesforce (medium)