PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/SoFi

Count numbers with same popcount up to all-ones bound

Last updated: May 10, 2026

Quick Overview

This question evaluates skill in bit manipulation and combinatorial counting over binary representations in the Coding & Algorithms domain, specifically reasoning about popcount and counting integers up to an all-ones bound.

  • easy
  • SoFi
  • Coding & Algorithms
  • Software Engineer

Count numbers with same popcount up to all-ones bound

Company: SoFi

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Take-home Project

## Problem For any positive integer `n`, define `f(n)` as the **smallest integer `>= n` whose binary representation consists only of `1` bits**. Equivalently, if `k` is the bit-length of `n` (so `2^(k-1) <= n <= 2^k - 1`), then: - `f(n) = 2^k - 1` (binary: `k` ones) You are given a positive integer `n`. Let `popcount(x)` be the number of set bits in `x`. ### Task Count how many integers `m` satisfy all of the following: - `1 <= m <= f(n)` - `m != n` - `popcount(m) = popcount(n)` Return this count. ### Input - A positive integer `n`. ### Output - An integer: the count of valid `m`. ### Constraints - `1 <= n <= 10^18` - Target time complexity: `O(log n)`. ### Examples 1) `n = 5` (binary `101`) - `k=3`, `f(n)=7` (binary `111`) - Numbers `<=7` with two set bits: `3(011), 5(101), 6(110)` - Excluding `n=5` → answer `2` 2) `n = 7` (binary `111`) - `k=3`, `f(n)=7` - Only number with three set bits is `7` itself → answer `0`

Quick Answer: This question evaluates skill in bit manipulation and combinatorial counting over binary representations in the Coding & Algorithms domain, specifically reasoning about popcount and counting integers up to an all-ones bound.

Related Interview Questions

  • Find Smallest Common Row Value - SoFi (easy)
  • Format words into wrapped/justified lines - SoFi (medium)
  • Find the second most frequent tag - SoFi (medium)
  • Implement a multithreaded task executor with semaphores - SoFi (medium)
  • Implement chance of a personal best - SoFi (hard)
SoFi logo
SoFi
Jan 7, 2026, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
14
0
Coding Console
Loading...

Problem

For any positive integer n, define f(n) as the smallest integer >= n whose binary representation consists only of 1 bits.

Equivalently, if k is the bit-length of n (so 2^(k-1) <= n <= 2^k - 1), then:

  • f(n) = 2^k - 1 (binary: k ones)

You are given a positive integer n. Let popcount(x) be the number of set bits in x.

Task

Count how many integers m satisfy all of the following:

  • 1 <= m <= f(n)
  • m != n
  • popcount(m) = popcount(n)

Return this count.

Input

  • A positive integer n .

Output

  • An integer: the count of valid m .

Constraints

  • 1 <= n <= 10^18
  • Target time complexity: O(log n) .

Examples

  1. n = 5 (binary 101 )
  • k=3 , f(n)=7 (binary 111 )
  • Numbers <=7 with two set bits: 3(011), 5(101), 6(110)
  • Excluding n=5 → answer 2
  1. n = 7 (binary 111 )
  • k=3 , f(n)=7
  • Only number with three set bits is 7 itself → answer 0

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More SoFi•More Software Engineer•SoFi Software Engineer•SoFi Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,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.