Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Sparse Matrix Class Implementation
00:00
5 left

Sparse Matrix Class Implementation

MediumPython

Problem

Describe how you would implement a sparse matrix class and the key operations you would support efficiently.

Implement sparse_matrix_operations(rows, cols, operations). Store only nonzero values. Each operation is a dictionary: set, add, get, transpose, multiply, or dense. Return one result per operation, using null for updates. A multiplication operand has rows, cols, and an entries list of [row, column, value].

The matrix dimensions are valid, indices are zero-based, and multiplication dimensions are compatible.

Constraints

  • 1 <= rows, cols <= 10^4
  • At most 10^5 nonzero entries are stored
  • 0 <= row < rows and 0 <= col < cols
  • Values are integers in the range -10^9 to 10^9
  • All multiplication operands have compatible dimensions
  • The input operation sequence contains at least one operation

Function Signature

def sparse_matrix_operations(rows, cols, operations):
Interviewer

Your question is Sparse Matrix Class Implementation. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.