PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithm design skills and proficiency with integer operations and decision-making under parity constraints, testing competencies such as bitwise reasoning, greedy and dynamic-programming intuition, and complexity analysis.

  • Medium
  • Salesforce
  • Coding & Algorithms
  • Software Engineer

Minimize steps to reduce integer

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a positive integer n (1 <= n <= 2^61 - 1), in one step you may replace n with n/2 if n is even, or with n+1 or n-1 if n is odd. Return the minimum number of steps to reduce n to 1. Explain your algorithm, justify how you decide between n+1 and n-1 for odd n, and analyze time complexity.

Quick Answer: This question evaluates algorithm design skills and proficiency with integer operations and decision-making under parity constraints, testing competencies such as bitwise reasoning, greedy and dynamic-programming intuition, and complexity analysis.

Given a positive integer n, reduce it to 1 using the minimum possible number of steps. In one step, you may replace n with n / 2 if n is even, or replace n with either n + 1 or n - 1 if n is odd. Return the minimum number of steps required. For odd numbers, the key decision is whether to increment or decrement. Except for n = 3, choose the operation that makes the result divisible by a larger power of 2. Practically, if n % 4 == 1, decrement n; if n % 4 == 3, increment n. The special case n = 3 should decrement to 2 because 3 -> 2 -> 1 takes 2 steps, while 3 -> 4 -> 2 -> 1 takes 3 steps.

Constraints

  • 1 <= n <= 2^61 - 1
  • Each operation must be one of: divide by 2 when even, add 1 when odd, or subtract 1 when odd

Examples

Input: (1,)

Expected Output: 0

Explanation: n is already 1, so no steps are needed.

Input: (2,)

Expected Output: 1

Explanation: 2 is even, so divide by 2: 2 -> 1.

Input: (3,)

Expected Output: 2

Explanation: The best path is 3 -> 2 -> 1. Incrementing first would take 3 steps: 3 -> 4 -> 2 -> 1.

Input: (8,)

Expected Output: 3

Explanation: Repeatedly divide by 2: 8 -> 4 -> 2 -> 1.

Input: (7,)

Expected Output: 4

Explanation: One optimal path is 7 -> 8 -> 4 -> 2 -> 1, which takes 4 steps.

Input: (15,)

Expected Output: 5

Explanation: The best path is 15 -> 16 -> 8 -> 4 -> 2 -> 1, which takes 5 steps.

Input: (2305843009213693951,)

Expected Output: 62

Explanation: This is 2^61 - 1. Increment once to get 2^61, then divide by 2 exactly 61 times.

Hints

  1. For an odd number, compare what happens after choosing n - 1 versus n + 1. Which one creates more trailing zero bits in binary?
  2. There is one important exception to the n % 4 rule: handle n = 3 separately.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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

  • Minimum Sum of Weekly Maximum Costs - Salesforce
  • Solve Two OA Coding Problems - Salesforce (medium)
  • 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)