← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta SWE coding round with a bit manipulation problem. Pretty focused session, just the one question but it had some depth to it.

Questions Asked (1)

Q1

Given a 128-bit integer, find the length of the longest consecutive sequence of 1s in its binary representation.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The 128-bit part is what trips you up at first.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and constraints, then propose an efficient bit-manipulation solution that scans the integer once. Discuss trade-offs between different approaches (e.g., iterative vs. bitwise tricks) and analyze time/space complexity.

Pro tip: Mention that you can use the expression `n & (n << 1)` to detect consecutive 1s, and that the problem can be solved in O(number of 1s) time by repeatedly clearing the lowest set bit. This shows deep bitwise knowledge and efficiency awareness.

1. Clarify the problem

Confirm that the integer is 128-bit, that we are looking for the longest run of consecutive 1s in its binary representation, and that the integer may be signed or unsigned. Ask about edge cases like all zeros or all ones.

2. Discuss brute-force approach

Explain a simple linear scan: convert to binary string or iterate bit by bit, keeping track of current and maximum run lengths. Mention its O(128) time and O(1) space complexity.

3. Propose optimized bitwise solution

Describe an O(k) approach where k is the number of 1s: repeatedly perform `n = n & (n << 1)` and count iterations until n becomes 0. Each iteration removes the least significant 1 from each consecutive run, effectively reducing the run lengths by 1.

4. Analyze complexity and trade-offs

Compare the brute-force and optimized methods: the brute-force is simpler and constant time (128 steps), while the optimized is faster for sparse integers but may be slower for dense ones. Discuss readability vs. performance.

5. Handle edge cases and test

Mention testing with 0, all 1s, alternating bits, and the maximum 128-bit value. Walk through a small example to verify correctness.

Key Points to Mention

  • Bitwise operations: AND, left shift, and their use in detecting consecutive 1s.
  • Time and space complexity analysis for each approach.
  • The trick `n & (n << 1)` to clear the lowest bit of each consecutive run.
  • Handling 128-bit integers in languages that don't natively support them (e.g., using two 64-bit integers or BigInteger).
  • Edge cases: zero, all ones, and integers with long runs.
  • Trade-offs between simplicity (brute-force) and efficiency (bitwise trick).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.