Flip Rows and Columns, Count Zeros
Updated
Problem statement
You have a binary matrix with n rows and m columns. Rows are numbered from 1 to n and columns from 1 to m. At the start, every cell is 0.
Each entry of queries is a string "t X i". Here t is the query type, X is R for a row or C for a column, and i is the row or column number.
- Type 1 (
"1 R i"or"1 C i"): flip every cell in rowior columni. A0becomes1and a1becomes0. - Type 2 (
"2 R i"or"2 C i"): count the cells in rowior columnithat are0right now.
Process the queries in order. Return the answers to the type 2 queries in the order they were asked. If there is no type 2 query, return an empty array.
Function
processMatrixQueries(n: int, m: int, queries: String[]) → int[]Examples
Example 1
n = 3m = 4queries = ["1 R 2","1 C 3","2 R 2","2 C 3","2 R 1","1 R 2","2 C 3"]return = [1,1,3,0]Row 2 is flipped, so it becomes 1 1 1 1. Then column 3 is flipped, which turns the matrix into rows 0 0 1 0, 1 1 0 1 and 0 0 1 0.
"2 R 2": row 2 is1 1 0 1, with one 0."2 C 3": column 3 holds 1, 0, 1, with one 0."2 R 1": row 1 is0 0 1 0, with three 0s.
Row 2 is flipped again and becomes 0 0 1 0. Now column 3 holds 1, 1, 1, so the last query gives 0. The answer is [1, 1, 3, 0].
Example 2
n = 1m = 5queries = ["1 C 1","1 C 5","1 C 1","1 R 1","2 R 1"]return = [1]Column 1 is flipped twice, so it ends where it started. Column 5 is flipped once, so the row is 0 0 0 0 1. Flipping row 1 gives 1 1 1 1 0, which has one 0. The answer is [1].
Example 3
n = 2m = 2queries = ["1 R 1","1 C 2"]return = []Both queries are flips and nothing is asked, so the answer is an empty array.
Constraints
1 <= n, m <= 10^51 <= queries.length <= 2 * 10^4- Each
queries[j]has the form"t X i"with single spaces, wheretis1or2andXisRorC. iis written without leading zeros. ForRqueries,1 <= i <= n. ForCqueries,1 <= i <= m.
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Repair One Movement InstructionOpenAI · OAOpenAI interview questions
- Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions
- Scaled Dot-Product AttentionMeshy · OA
- Count Straight-Line Word Occurrences in a GridVisa · OAVisa interview questions
- Find Last CharacterGoogle · OAGoogle interview questions
- Count Prioritized Binary-Run OperationsZipRecruiter · OAZipRecruiter interview questions
- Hidden ArtifactsIMC Trading · OAIMC Trading interview questions
- Balance a Chemical EquationMicrosoft · OAMicrosoft interview questions
- Simulate a Deterministic Finite AutomatonPoint72 · OAPoint72 interview questions
- Collect Letters Along a Unique Grid PathApple · OAApple interview questions