Problem · Prefix Sum

Binary String Requests

Learn this problem
MediumDatabricks logoDatabricksINTERNOA

Problem statement

You are given a binary string and a sequence of requests:

  • flip changes every 0 to 1 and every 1 to 0.
  • count:<index> counts the zeroes from position 0 through the given 0-based index, inclusive, in the string's current state.

Return the answers to the count requests in their processing order.

Function

databricksStringRequests(binaryString: String, requests: String[]) → int[]

Examples

Example 1

binaryString = "1111010"requests = ["count:4", "count:6", "flip", "count:4", "flip", "count:2"]return = [1, 2, 4, 0]

The first two prefixes contain 1 and 2 zeroes. After a flip, the prefix ending at index 4 contains 4 zeroes. The second flip restores the original string, whose prefix ending at index 2 contains none.

Constraints

🫎🫎

More Databricks problems

drafts saved locally
public int[] databricksStringRequests(String binaryString, String[] requests) {
  // write your code here
}
binaryString"1111010"
requests["count:4", "count:6", "flip", "count:4", "flip", "count:2"]
expected[1, 2, 4, 0]
checking account