Count ones in a binary string after repeated whole-string flips

Quick Overview

Process a sequence of count and flip queries on a binary string, where a flip inverts every bit and a count reports how many ones the string currently has. It tests spotting repeated work and answering every query in constant time after one pass over the input.

Count ones in a binary string after repeated whole-string flips

Company: Okta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a binary string `s` and a list of queries. Process the queries in order. Each query is one of two types: - `"count"`: report the number of `'1'` characters in the current string. - `"flip"`: invert every bit, so each `'0'` becomes `'1'` and each `'1'` becomes `'0'`. Return a list with the answers to the `"count"` queries, in the order those queries appear. ### Function Signature ```python def flip_and_count(s: str, queries: list[str]) -> list[int]: ``` ### Rules - A `"flip"` query changes the string for every later query. - If there are no `"count"` queries, return an empty list. ### Constraints - `1 <= len(s) <= 10^5` - `1 <= len(queries) <= 10^5` - Each `queries[i]` is either `"count"` or `"flip"`. - `s` consists only of `'0'` and `'1'`. ### Examples ```text Input: s = "10110", queries = ["count", "flip", "count", "flip", "count"] Output: [3, 2, 3] ``` The string starts with 3 ones. After the first flip it is `"01001"`, which has 2 ones. The second flip restores `"10110"`, so the last count is 3 again. ```text Input: s = "0000", queries = ["count", "flip", "count"] Output: [0, 4] ``` The string has no ones at first. After the flip it is `"1111"`, which has 4. ```text Input: s = "1", queries = ["flip", "flip", "flip", "count"] Output: [0] ``` Three flips leave the string inverted as `"0"`, so the only count is 0.

Overview: Process a sequence of count and flip queries on a binary string, where a flip inverts every bit and a count reports how many ones the string currently has. It tests spotting repeated work and answering every query in constant time after one pass over the input.

Okta logo
Okta
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a binary string s and a list of queries. Process the queries in order. Each query is one of two types:

  • "count" : report the number of '1' characters in the current string.
  • "flip" : invert every bit, so each '0' becomes '1' and each '1' becomes '0' .

Return a list with the answers to the "count" queries, in the order those queries appear.

Function Signature

def flip_and_count(s: str, queries: list[str]) -> list[int]:

Rules

  • A "flip" query changes the string for every later query.
  • If there are no "count" queries, return an empty list.

Constraints

  • 1 <= len(s) <= 10^5
  • 1 <= len(queries) <= 10^5
  • Each queries[i] is either "count" or "flip" .
  • s consists only of '0' and '1' .

Examples

Input: s = "10110", queries = ["count", "flip", "count", "flip", "count"]
Output: [3, 2, 3]

The string starts with 3 ones. After the first flip it is "01001", which has 2 ones. The second flip restores "10110", so the last count is 3 again.

Input: s = "0000", queries = ["count", "flip", "count"]
Output: [0, 4]

The string has no ones at first. After the flip it is "1111", which has 4.

Input: s = "1", queries = ["flip", "flip", "flip", "count"]
Output: [0]

Three flips leave the string inverted as "0", so the only count is 0.