Count median in grid

Last updated: May 9, 2026

Quick Overview

Given a 2D grid of integers, write a function to calculate the median value of all the elements in the grid. The function should take the grid as input and return the median as output, ensuring that the solution handles grids of varying sizes and values efficiently.

Salesforce
Coding & Algorithms
Software Engineer
Salesforce
May 9, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

20

1

2,007 solved


Given a 2D grid of integers, write a function to calculate the median value of all the elements in the grid. The function should take the grid as input and return the median as output, ensuring that the solution handles grids of varying sizes and values efficiently.

Salesforce uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Data structure selection and trade-offs
Time and space complexity analysis
Graph algorithms and traversal
Dynamic programming and memoization
Hash maps and frequency counting
Sorting and searching
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the worst-case input for your solution?
  • How would you parallelize this solution?
  • Can you solve this in a single pass?
  • How would you modify your solution to handle streaming input?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To find the median of a 2D grid of integers, we need to first understand the definition of the median in a dataset: it's the middle value when the numbers are sorted. If the size of the dataset is odd...

Approach
  1. Flatten the Grid: Convert the 2D grid into a 1D list of integers. For instance, given the grid: [[1, 5, 3], [7, 8, 9], [2, 4, 6]], the flattened list would be [1, 5, 3, 7, 8, 9...

Submit Your Answer
Markdown supported

Related Questions