← AlgoMindset 75

Counting Bits

Easy🎙 Interview me on this

🧠 Before you code — what pattern fits?

Given an integer n, return an array ans of length n + 1 such that ans[i] is the number of 1's in the binary representation of i.

n=2 → [0,1,1]

n=5 → [0,1,1,2,1,2]

🎬 Video explanation · NeetCode

Your solution runs against 2 unit tests, including edge and large-value cases.

Loading...

Test console · 2 unit tests

Submit your code to run it against all 2 unit tests. Results appear here.

JavaScript, TypeScript & Python run sandboxed in your browser; other languages run on the execution server. Your code is saved locally as you type.