PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of prefix-sum precomputation for constant-time range queries and the design of a max-enabled stack, focusing on data structure design, algorithmic complexity, and space-time trade-offs.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Design prefix-sum function and max stack

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

##### Question LeetCode 303. Range Sum Query – Immutable (design a reusable prefix-sum function) LeetCode 716. Max Stack (similar design question) https://leetcode.com/problems/range-sum-query-immutable/description/ https://leetcode.com/problems/max-stack/description/

Quick Answer: This question evaluates understanding of prefix-sum precomputation for constant-time range queries and the design of a max-enabled stack, focusing on data structure design, algorithmic complexity, and space-time trade-offs.

Design a function that processes a sequence of operations on a Max Stack. The stack supports the following commands: (1) "push x": push integer x onto the stack; (2) "pop": remove and return the top element; (3) "top": return the top element without removing it; (4) "peekMax": return the current maximum element; (5) "popMax": remove and return the maximum element; if multiple maxima exist, remove the one closest to the top. The function must return a list of integers representing the results of all operations that produce output (pop, top, peekMax, popMax), in order. All inputs are valid; operations that require a non-empty stack will not be called on an empty stack.

Constraints

  • 1 <= len(operations) <= 20000
  • Operations are one of: "push x", "pop", "top", "peekMax", "popMax"
  • -10^9 <= x <= 10^9
  • Operations that require a non-empty stack (pop, top, peekMax, popMax) will not be called on an empty stack

Hints

  1. Maintain a parallel stack that tracks the maximum at each depth.
  2. For popMax, move elements to a temporary buffer until the maximum is at the top, remove it, then restore the buffered elements.
  3. When restoring elements from the buffer, use the same push logic so the max-tracking structure stays correct.
Last updated: Mar 29, 2026

Loading coding console...

PracHub

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

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.

Related Coding Questions

  • Validate Sorted Order Under a Custom Alphabet - Meta (medium)
  • Palindrome After Deleting at Most One Character - Meta (medium)
  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)