PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Pinterest

Implement sparse matrix storage, addition, and multiplication

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of sparse matrix representations, algorithmic efficiency for linear algebra operations, and the ability to manipulate sparse data structures and aggregate non-zero entries.

  • medium
  • Pinterest
  • Coding & Algorithms
  • Software Engineer

Implement sparse matrix storage, addition, and multiplication

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Design a way to store a sparse matrix (most entries are zero) and implement efficient operations. You are given matrices using their non-zero entries: - Matrix `A` has shape `(m, n)` and is represented as a list of triples `(row, col, value)` containing only entries where `value != 0`. - Matrix `B` has shape `(n, p)` (for multiplication) and/or shape `(m, n)` (for addition), represented the same way. Implement the following: 1. **Sparse storage**: Choose a representation suitable for efficient operations. 2. **Addition**: Compute `A + B` (only when shapes match), returning the result in the same sparse triple-list form (do not output explicit zeros). 3. **Multiplication**: Compute `A × B`, returning the result in sparse triple-list form. **Constraints** - Dimensions can be large (e.g., up to `1e5` in each direction), but the number of non-zero entries `k` is much smaller than `m*n`. - Input triples may not be sorted. **Output requirements** - Return only non-zero entries. - You may return triples in any order unless otherwise specified.

Quick Answer: This question evaluates understanding of sparse matrix representations, algorithmic efficiency for linear algebra operations, and the ability to manipulate sparse data structures and aggregate non-zero entries.

Related Interview Questions

  • Design Hierarchical Permission Checks - Pinterest (medium)
  • Implement weighted random choice - Pinterest (medium)
  • Solve five hard algorithm problems - Pinterest
  • Sample a string by real-valued scores - Pinterest (hard)
  • Find First Prefix-Matching Word - Pinterest (medium)
Pinterest logo
Pinterest
Jan 6, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
7
0
Loading...

Design a way to store a sparse matrix (most entries are zero) and implement efficient operations.

You are given matrices using their non-zero entries:

  • Matrix A has shape (m, n) and is represented as a list of triples (row, col, value) containing only entries where value != 0 .
  • Matrix B has shape (n, p) (for multiplication) and/or shape (m, n) (for addition), represented the same way.

Implement the following:

  1. Sparse storage : Choose a representation suitable for efficient operations.
  2. Addition : Compute A + B (only when shapes match), returning the result in the same sparse triple-list form (do not output explicit zeros).
  3. Multiplication : Compute A × B , returning the result in sparse triple-list form.

Constraints

  • Dimensions can be large (e.g., up to 1e5 in each direction), but the number of non-zero entries k is much smaller than m*n .
  • Input triples may not be sorted.

Output requirements

  • Return only non-zero entries.
  • You may return triples in any order unless otherwise specified.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Pinterest•More Software Engineer•Pinterest Software Engineer•Pinterest Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.