DSA interview question
A permissions system stores each user's rights as bits of one integer: bit 0 = read, bit 1 = write, bit 2 = delete, and so on. Before an action runs, the server must answer "is bit k of this user's permission mask set?" millions of times a second. The answer is a single shift and mask: move bit k down to position 0 and check it, or build a mask with only bit k set and AND it with the number.
Example: n = 4 (binary 100), k = 2 → true; k = 0 → false.
bool isKthBitSet(int n, int k); // k = 0 is the least significant bit, 0 <= k <= 31
Input (n, k) | Output | Explanation |
|---|---|---|
(4, 0) | false | 4 = 100 |
(4, 2) | true | |
(500, 3) | false | 500 = 111110100 |
(500, 4) | true | |
(-1, 31) | true | The sign bit of −1 is set |
(Confirm: bits are numbered from 0 at the least significant end; n may be negative, in which case its two's complement bits are checked.)
1 << 31?"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