BackmediumArraysRazorpay

Priority Crate Management Solution

Problem Statement

Implement a CrateManager class that utilizes a stack data structure to manage a collection of crates, where each crate is represented by its value.

Example 1
Input
addCrate(5), addCrate(3), removeCrate(), peekHighestPriority()
Output
[null, 5, 5]

Explanation: Step 1: Add crate 5 to the stack. The stack is now [5]. Step 2: Add crate 3 to the stack. The stack is now [3, 5]. Step 3: Remove crate 3 from the stack. The stack is now [5]. Step 4: Peek the highest priority crate from the stack. The highest priority crate is 5.

Example 2
Input
addCrate(5), addCrate(5), removeCrate(), peekHighestPriority()
Output
[null, 5, 5]

Explanation: Step 1: Add crate 5 to the stack. The stack is now [5]. Step 2: Add crate 5 to the stack. The stack is now [5, 5]. Step 3: Remove crate 5 from the stack. The stack is now [5]. Step 4: Peek the highest priority crate from the stack. The highest priority crate is 5.

Constraints

  • At most 10^4 crates are added to or removed from the inventory.
  • The value of each crate is between 1 and 10^5.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Priority Crate Management — Problem Statement & Solution Guide

ArraysMediumStack LIFO
TimeO(1) for addCrate(), peekHighestPriority(), and removeCrate() operations when the stack is not empty. O(n) for addCrate() and removeCrate() operations when the stack is empty because we need to push/pop all elements.
|
SpaceO(n)

Problem Description

Implement a CrateManager class that utilizes a stack data structure to manage a collection of crates, where each crate is represented by its value.

Examples

Example 1

Input

addCrate(5), addCrate(3), removeCrate(), peekHighestPriority()

Output

[null, 5, 5]

Explanation: Step 1: Add crate 5 to the stack. The stack is now [5]. Step 2: Add crate 3 to the stack. The stack is now [3, 5]. Step 3: Remove crate 3 from the stack. The stack is now [5]. Step 4: Peek the highest priority crate from the stack. The highest priority crate is 5.

Example 2

Input

addCrate(5), addCrate(5), removeCrate(), peekHighestPriority()

Output

[null, 5, 5]

Explanation: Step 1: Add crate 5 to the stack. The stack is now [5]. Step 2: Add crate 5 to the stack. The stack is now [5, 5]. Step 3: Remove crate 5 from the stack. The stack is now [5]. Step 4: Peek the highest priority crate from the stack. The highest priority crate is 5.

Constraints

  • At most 10^4 crates are added to or removed from the inventory.
  • The value of each crate is between 1 and 10^5.

Optimal Approach & Strategy

The optimized approach involves using a stack to manage the inventory and keeping track of the highest priority crate, allowing for efficient retrieval with a time complexity of O(1).

Brute Force Approach

A brute-force approach would involve searching the entire inventory for the highest priority crate every time getHighestPriorityCrates is called, resulting in a time complexity of O(n). This approach is inefficient as the inventory size increases.

Verified Code Solutions

JavaScript Solution
Time: O(1) for addCrate(), peekHighestPriority(), and removeCrate() operations when the stack is not empty. O(n) for addCrate() and removeCrate() operations when the stack is empty because we need to push/pop all elements.
class CrateManager {
  constructor() {
    this.stack = [];
  }

  addCrate(crate) {
    if (this.stack.length === 0) {
      this.stack.push(crate);
    } else if (this.stack[this.stack.length - 1] < crate) {
      this.stack.push(crate);
    }
  }

  removeCrate() {
    if (this.stack.length > 0) {
      return this.stack.pop();
    } else {
      return null;
    }
  }

  peekHighestPriority() {
    if (this.stack.length > 0) {
      return this.stack[this.stack.length - 1];
    } else {
      return null;
    }
  }
}

Asked in Top Tech Interviews

Razorpay

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.