Bit Manipulation in Java Interviews: 7 Tricks That Turn O(n) Into O(1)
Every int in Java is already a 32-bit array, and the interview questions in this post are all the same question in different clothes: which identity about those bits turns a counting problem into a one-pass problem? A HashMap counts; a bit trick does not need to, because the bits already carry the information. The seven tricks below are identities, not algorithms — each one is a line of algebra with a proof you can do on a whiteboard — and each one solves a real LeetCode question (eight in all, since one trick has a one-line corollary) in O(n) time and O(1) space. The HashMap versions all work too, and the post says exactly where they lose: one integer of state instead of a table. ...