← Salesforce Interview Insights

Salesforce·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Salesforce SWE interview with a single coding question, nothing too wild but it's the kind of thing that trips you up if you haven't thought about bit manipulation recently.

Questions Asked (1)

Q1

Write a function to determine whether a given number is a power of two.

Algorithms & Data Structures
Author's notes

My first instinct was to just loop and keep dividing by 2, which works fine but felt clunky.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying edge cases (e.g., zero, negative numbers) and then present a bitwise solution: a positive integer n is a power of two if and only if n > 0 and (n & (n - 1)) == 0. Explain why this works and mention the time and space complexity.

Pro tip: Mention that this bitwise trick is a common interview question and that you would also handle edge cases like n <= 0; showing awareness of integer overflow or language-specific behavior (e.g., in Python, negative numbers have infinite bits) can impress the interviewer.

1. Clarify requirements and edge cases

Ask whether the input can be negative, zero, or non-integer, and confirm the expected return type (boolean).

2. Propose a naive approach

Briefly mention a loop that repeatedly divides by 2, but note it's less efficient than the bitwise method.

3. Present the optimal bitwise solution

Explain that for positive integers, n & (n - 1) clears the lowest set bit; if the result is 0 and n > 0, n is a power of two.

4. Analyze complexity and edge cases

State that the bitwise approach runs in O(1) time and O(1) space, and explicitly handle n <= 0 by returning false.

5. Test with examples

Walk through examples like n=1 (true), n=2 (true), n=3 (false), n=0 (false), n=-4 (false) to verify correctness.

Key Points to Mention

  • Bitwise AND operation: n & (n - 1) removes the lowest set bit.
  • Condition: n > 0 and (n & (n - 1)) == 0.
  • Time complexity O(1) and space complexity O(1).
  • Edge cases: zero, negative numbers, and non-integer inputs.
  • Alternative approaches: loop division or logarithm, but bitwise is optimal.
  • Language-specific considerations (e.g., Python's arbitrary precision integers).

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