Count combinations of 4-wheelers and 2-wheelers that use exactly n wheels

Quick Overview

Given a total number of wheels, count how many distinct mixes of 4-wheel and 2-wheel vehicles use exactly that many wheels. It tests turning a counting task into a linear equation over non-negative integers and handling parity and the zero case in constant time.

Count combinations of 4-wheelers and 2-wheelers that use exactly n wheels

Company: DocuSign

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a non-negative integer `n`, the total number of wheels, return the number of distinct combinations of 4-wheeler vehicles (4 wheels each) and 2-wheeler vehicles (2 wheels each) that together use exactly `n` wheels. A combination is defined by its count of 4-wheelers and its count of 2-wheelers. Order does not matter. Both vehicle types use an even number of wheels, so an odd `n` has no valid combination. ### Function Signature ```python def count_combinations(n: int) -> int: ``` ### Rules - A combination is a pair `(a, b)` of non-negative integers with `4 * a + 2 * b == n`, where `a` is the number of 4-wheelers and `b` is the number of 2-wheelers. - Two combinations are the same only when both counts match. - For `n = 0` the answer is 1: the combination with zero vehicles of each type. ### Constraints - `0 <= n <= 10^9` ### Examples ```text Input: n = 8 Output: 3 ``` The valid combinations (4-wheelers, 2-wheelers) are (0, 4), (1, 2) and (2, 0). ```text Input: n = 7 Output: 0 ``` `n` is odd, and any mix of vehicles that each have an even number of wheels has an even total. ```text Input: n = 6 Output: 2 ``` The valid combinations are (0, 3) and (1, 1). Two 4-wheelers would already need 8 wheels.

Overview: Given a total number of wheels, count how many distinct mixes of 4-wheel and 2-wheel vehicles use exactly that many wheels. It tests turning a counting task into a linear equation over non-negative integers and handling parity and the zero case in constant time.

DocuSign logo
DocuSign
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a non-negative integer n, the total number of wheels, return the number of distinct combinations of 4-wheeler vehicles (4 wheels each) and 2-wheeler vehicles (2 wheels each) that together use exactly n wheels.

A combination is defined by its count of 4-wheelers and its count of 2-wheelers. Order does not matter. Both vehicle types use an even number of wheels, so an odd n has no valid combination.

Function Signature

def count_combinations(n: int) -> int:

Rules

  • A combination is a pair (a, b) of non-negative integers with 4 * a + 2 * b == n , where a is the number of 4-wheelers and b is the number of 2-wheelers.
  • Two combinations are the same only when both counts match.
  • For n = 0 the answer is 1: the combination with zero vehicles of each type.

Constraints

  • 0 <= n <= 10^9

Examples

Input: n = 8
Output: 3

The valid combinations (4-wheelers, 2-wheelers) are (0, 4), (1, 2) and (2, 0).

Input: n = 7
Output: 0

n is odd, and any mix of vehicles that each have an even number of wheels has an even total.

Input: n = 6
Output: 2

The valid combinations are (0, 3) and (1, 1). Two 4-wheelers would already need 8 wheels.