Query and Matrix Problem Statement

You are given a binary matrix with 'M' rows and 'N' columns, initially consisting of all 0s. You will receive 'Q' queries, which can be of four types:

Query 1: 1 R index
Query 2: 1 C index
Query 3: 2 R index
Query 4: 2 C index

In each query, the first input is the type of the query, the second input indicates whether to consider the row ('R') or the column ('C'), and the third input is the index of the row/column.

For each type 1 query, flip the elements of the row/column at the given index. For each type 2 query, output the number of zeros present in the row/column at the given index.

Example:

Input:
M = 3, N = 3
Queries: 1R1, 1R2, 2C1
Output:
1
Explanation:

The matrix changes as per the queries, and the final query 2C1 returns the count of zeros in the 1st column, which is 1.

Constraints:

  • 1 <= T <= 10
  • 1 <= M, N <= 100
  • 1 <= Q <= 1000
  • 1 <= R <= M
  • 1 <= C <= N
  • type = 1, 2
  • Time limit: 1 sec
Note:
Note that the matrix is a binary matrix, meaning it only contains either 0 or 1. You don’t need to print anything; it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Given a binary matrix, perform queries to flip elements in rows/columns and count zeros in rows/columns.

  • Iterate through each query and update the matrix accordingly

  • For type 1 queries, flip the element...read more

Help your peers!
Select
Add answer anonymously...
DBS Bank Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits