Code RoomCount set bits
EasyPrep Room Coding #974

Count set bits

CodingAlgorithms & data structuresEntry–Mid~15 min

Given an integer n (0 <= n <= 1e5), return a list ans of length n+1 where ans[i] is the number of set bits (1s) in the binary representation of i. Aim for O(n) total time using the relationship between i and a smaller, already-computed value (no per-number popcount loop).

Implement
count_bits(n: int) → list[int]
Examples
in[2]out[0,1,1]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 15 min
InputExpectedGot
[2][0,1,1]not run yetsample