PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

Count representations of a positive integer as one or more consecutive positive integers, including the one-term representation. Derive the arithmetic condition for each sequence length and use a square-root bound rather than testing every possible starting value.

  • hard
  • Airbnb
  • Coding & Algorithms
  • Software Engineer

Count Consecutive Positive Integer Sums

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

# Count Consecutive Positive Integer Sums Given a positive integer `n`, return the number of ways to write `n` as a sum of one or more consecutive positive integers. Two representations are different if they have different starting values or lengths. The one-term representation `n` itself counts. ### Function Signature ```python def count_consecutive_sums(n: int) -> int: ``` ### Examples ```text Input: n = 5 Output: 2 Explanation: 5 and 2 + 3 Input: n = 15 Output: 4 Explanation: 15, 7 + 8, 4 + 5 + 6, and 1 + 2 + 3 + 4 + 5 ``` ### Constraints - `1 <= n <= 10^12` - Every term in a representation must be a positive integer. - Return an integer count; do not return the representations themselves. ### Clarifications - Terms must increase by exactly one. - Order is fixed by consecutiveness, so reversing a sequence is not another representation. ### Hints - For a chosen sequence length, express the sum using its first value. - Use arithmetic bounds to avoid checking all possible starting values.

Quick Answer: Count representations of a positive integer as one or more consecutive positive integers, including the one-term representation. Derive the arithmetic condition for each sequence length and use a square-root bound rather than testing every possible starting value.

Return the number of representations of a positive integer as one or more consecutive positive integers. The one-term representation counts, and only the count—not the sequences—is returned.

Constraints

  • n is a positive integer no greater than 10^12.
  • Every term must be positive.
  • Terms increase by exactly one.
  • Different starts or lengths are distinct.
  • Return an exact integer count.

Examples

Input: (1,)

Expected Output: 1

Explanation: One is represented only by itself.

Input: (2,)

Expected Output: 1

Explanation: Two has only its one-term representation.

Hints

  1. The answer equals the number of odd divisors of n.
  2. Remove factors of two before factoring the odd part.
  3. For a prime exponent e, multiply the divisor count by e + 1.
Last updated: Jul 15, 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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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 Canisters to Top Up an Exact Target - Airbnb (medium)
  • Count Candies from Unlockable Boxes - Airbnb (medium)
  • Find Optimal Property Combination - Airbnb (medium)
  • Flatten a Nested Integer List - Airbnb (hard)