DSA interview question
Before your team lets you touch any Trie-based bit manipulation problems (Max XOR, Max XOR with constraints, etc.), your tech lead wants to confirm you're fluent in the bitwise building blocks these problems rely on. This is a theory/fundamentals check — not a single coded problem, but a set of core operations and concepts you need cold: extracting bits, setting/clearing bits, counting bits, understanding two's complement, and how XOR's properties make it ideal for Trie-based "maximize/minimize" problems.
(Theory-style — here are the standard utility functions interviewers expect you to write fluently and explain.)
int getBit(int num, int i); // get value of i-th bit (0 or 1)
int setBit(int num, int i); // set i-th bit to 1
int clearBit(int num, int i); // set i-th bit to 0
int toggleBit(int num, int i); // flip i-th bit
int countSetBits(int num); // count total 1-bits (popcount)
bool isPowerOfTwo(int num); // check if num is a power of 2
int highestSetBitPosition(int num); // index of the most significant 1-bit
| Operation | Input | Output | Explanation |
|---|---|---|---|
getBit(num=10, i=1) | 10 = 1010 | 1 | Bit at position 1 (0-indexed from LSB) is 1 |
setBit(num=10, i=0) | 10 = 1010 | 11 | 1010 -> 1011 |
clearBit(num=10, i=1) | 10 = 1010 | 8 | 1010 -> 1000 |
toggleBit(num=10, i=0) | 10 = 1010 | 11 | 1010 -> 1011 (bit 0 was 0, becomes 1) |
countSetBits(num=7) | 7 = 0111 | 3 | Three 1-bits |
isPowerOfTwo(num=16) | 16 = 10000 | true | Exactly one set bit |
isPowerOfTwo(num=18) | 18 = 10010 | false | Two set bits |
highestSetBitPosition(num=13) | 13 = 1101 | 3 | Most significant 1 is at index 3 (0-indexed from LSB) |
BITS = 31 for values up to 2^31-1.)n & (n-1) clear the lowest set bit? Walk through why."n & (n-1) == 0 a valid power-of-two check?"On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in