DSA interview question
A load balancer alternates requests between two server pools based on whether the request ID is odd or even. The check runs on every request and must be correct for negative IDs too, which some legacy clients send. n % 2 == 1 is a classic trap: in C++, -3 % 2 is -1, so negative odd numbers are misclassified. Looking at the least significant bit avoids both the division and the sign problem: in two's complement, a number is odd exactly when bit 0 is 1.
Example: 7 → odd; −4 → even.
bool isOdd(int n);
| Input | Output | Explanation |
|---|---|---|
7 | odd | 111 ends in 1 |
10 | even | 1010 ends in 0 |
0 | even | |
-3 | odd | Two's complement ...1101 ends in 1, even though −3 % 2 is −1 |
-4 | even | ...1100 |
(Confirm: n is a 32-bit signed integer, possibly negative.)
n & 1 really faster?"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