Array of 10000 having 16bit elements, find bits set unlimited RAM - Google interview
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This interview question looks simple, but it tests whether you can choose the right bit counting strategy for the actual usage pattern. With 10,000 values of 16 bits, a direct scan is already fast. The interesting part is explaining alternatives for repeated queries and showing clear tradeoffs.
Clarify The Problem Before Coding
Interviewers often expect you to ask one key question first: do we need the count once, or many times for changing ranges. The answer changes the best solution.
- Single total count over the whole array: one pass with popcount is enough.
- Many range queries: preprocess prefix sums of bit counts.
- Many updates plus many queries: use a Fenwick tree or segment tree over per index popcount.
Because each element is 16 bit wide, you can also use a lookup table of size 65,536 when memory is not a concern.
Baseline Solution With Built In Popcount
The cleanest baseline is one pass and a CPU optimized popcount intrinsic.
This runs in linear time and constant extra space. For only 10,000 numbers, this is usually the expected first answer.
Fast Repeated Queries With Prefix Bit Counts
If the interviewer changes the question to many range queries, preprocess once.
Preprocessing is linear. Each query is constant time.
Lookup Table For 16 Bit Values
Because values are limited to 16 bits, you can precompute popcount for every possible value once and then use table lookup.
This is still linear in array length, but each element count becomes a very cheap memory read.
How To Talk Through Complexity In Interviews
A strong interview answer usually presents two layers.
- Immediate correct baseline with clear complexity.
- Upgrade path when query pattern changes.
For this question:
- Baseline: linear scan and popcount.
- Range query upgrade: prefix sums.
- Heavy update plus query upgrade: Fenwick tree on bit counts.
- Memory rich micro optimization: 16 bit lookup table.
That demonstrates algorithm selection, not only bit tricks.
Edge Cases Worth Mentioning
Mentioning edge cases shows practical maturity.
- Ensure values are treated as 16 bit, especially if input type is wider.
- Avoid signed shift assumptions in low level languages.
- Validate query ranges before prefix subtraction.
- Use a wide enough accumulator if array size can grow beyond the prompt.
Common Pitfalls
- Over engineering with complex trees before confirming query requirements.
- Forgetting that
osizeis tiny here and baseline already passes comfortably. - Using floating point or string conversions for bit counting.
- Ignoring integer width and accidentally counting beyond 16 bits.
- Claiming an optimization without stating time and space impact.
Summary
- For one total count, a single pass with popcount is the best first answer.
- For repeated range queries, prefix bit counts give constant time queries.
- With unlimited RAM and 16 bit values, a 65,536 entry lookup table is viable.
- Always adapt the method to query and update patterns.
- Interview strength comes from clear tradeoff reasoning plus correct code.

