FastPrepMinimum Operations to Flip Exactly K Bits
Problems/Minimum Operations to Flip Exactly K Bits

Minimum Operations to Flip Exactly K Bits

Google logoGoogle● HardINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The firsthand report says each operation flips exactly K elements, and you want the fewest operations to make everything one. It does not give input limits or say how positions are picked. Here, you pick K distinct indices of your choice. Our version is about 96% the same as the reported question.

You are given a binary array bits and an integer k.

In one operation, choose exactly k distinct indices in any positions of the array and flip their values:

  • 0 becomes 1.
  • 1 becomes 0.

Return the minimum number of operations needed to make every array element equal to 1. If this is impossible, return -1.

Function

minExactKBitFlips(bits: int[], k: int) → int

Examples

Example 1

bits = [0,0,1]k = 2return = 1

Flip the two zero-valued positions. The array becomes [1,1,1] in one operation.

Example 2

bits = [0,1,1]k = 2return = -1

Every operation flips an even number of bits, so the parity of the zero count cannot change from odd to zero.

Example 3

bits = [0,0,0,0]k = 3return = 4

Four operations suffice by omitting a different index each time. Every index is then flipped exactly three times. Fewer operations cannot satisfy both the required parity and the number of selected positions.

Example 4

bits = [1,1,1]k = 1return = 0

The array already contains only ones, so no operation is needed.

Constraints

  • 1 <= bits.length <= 10^5.
  • bits[i] is either 0 or 1.
  • 1 <= k <= bits.length.

More Google Online Assessment questions

  • Minimum Operations to Make a SubsequenceOA · Seen Nov 2026
  • Book Reading EveningsOA · Seen Nov 2026
  • Count Score CategoriesOA · Seen Nov 2026
  • Minimum Cars for Rental RequestsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Shortest Path in a Binary MatrixOA · Seen Nov 2026
  • Robot Assembly Line ConflictsOA · Seen Oct 2026
  • Assign LockerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Biggest Number From DigitsOA · Seen Oct 2026
  • Consolidate On-Call RotationsOA · PHONE SCREEN · Seen Oct 2026
  • Decreasing SubsequencesOA · Seen Sep 2026

Similar questions at other companies

  • Next Pascal Row CoefficientIntuit · OAIntuit interview questions
  • Get Triplet CountIBM · OAIBM interview questions
  • Sum of Pipeline Response TimesBloomberg LP · OABloomberg LP interview questions
  • Subarray SumVisa · OAVisa interview questions
  • Get Triplet CountPoint72 · OAPoint72 interview questions
  • Product of Subset MaximaArcesium · OAArcesium interview questions
  • Find Maximum Number of Balanced CombinationsAmazon · OA
  • Counting Triplets Divisible by DAT&T · OAAT&T interview questions
  • Find Max Number of PairsRoblox · OARoblox interview questions
  • Count Valid TeamsWells Fargo · OAWells Fargo interview questions

Interview reports that mention this question

  • Google OA: What It Tests (2026)
  • Google SWE Intern OA and Interviews: 42 Experiences (Part 1 of 3)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int minExactKBitFlips(int[] bits, int k) {
        // Write your code here.
    }
}
Checking account…