BackmediumStackSalesforce

Validate Crate Sequences Solution

Problem Statement

You are given a sequence of crate operations, where each operation is either 'add' or 'remove' with a corresponding crate type. Determine if the sequence is valid. A sequence is valid if every crate addition can be matched with a corresponding crate removal, and at no point is a crate removed from an empty stack or a crate added on top of another crate of the same type.

Example 1
Input
['add crate1', 'add crate2', 'remove crate1', 'remove crate2', 'add crate1', 'remove crate1']
Output
false

Explanation: Step-by-step: 1. We start with an empty stack. 2. We add crate1 and crate2 to the stack. 3. We remove crate1 and crate2 from the stack. 4. We try to add crate1 on top of crate2 of the same type, which is invalid. 5. We try to remove crate1 from an empty stack, which is also invalid.

Example 2
Input
['remove crate1', 'add crate1', 'remove crate1', 'add crate1', 'remove crate1']
Output
false

Explanation: Step-by-step: 1. We start with an empty stack. 2. We try to remove crate1 from an empty stack, which is invalid. 3. We add crate1 to the stack. 4. We remove crate1 from the stack. 5. We add crate1 to the stack again. 6. We remove crate1 from the stack again.

Constraints

  • 1 <= sequence length <= 100
  • Each operation in the sequence is either 'add_X' or 'remove_X', where X is a crate type (A, B, C, etc.)
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

Validate Crate Sequences — Problem Statement & Solution Guide

StackMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

You are given a sequence of crate operations, where each operation is either 'add' or 'remove' with a corresponding crate type. Determine if the sequence is valid. A sequence is valid if every crate addition can be matched with a corresponding crate removal, and at no point is a crate removed from an empty stack or a crate added on top of another crate of the same type.

Examples

Example 1

Input

['add crate1', 'add crate2', 'remove crate1', 'remove crate2', 'add crate1', 'remove crate1']

Output

false

Explanation: Step-by-step: 1. We start with an empty stack. 2. We add crate1 and crate2 to the stack. 3. We remove crate1 and crate2 from the stack. 4. We try to add crate1 on top of crate2 of the same type, which is invalid. 5. We try to remove crate1 from an empty stack, which is also invalid.

Example 2

Input

['remove crate1', 'add crate1', 'remove crate1', 'add crate1', 'remove crate1']

Output

false

Explanation: Step-by-step: 1. We start with an empty stack. 2. We try to remove crate1 from an empty stack, which is invalid. 3. We add crate1 to the stack. 4. We remove crate1 from the stack. 5. We add crate1 to the stack again. 6. We remove crate1 from the stack again.

Constraints

  • 1 <= sequence length <= 100
  • Each operation in the sequence is either 'add_X' or 'remove_X', where X is a crate type (A, B, C, etc.)

Optimal Approach & Strategy

The optimal approach involves utilizing a stack to track the added crates and checking for corresponding removals, ensuring that no two crates of the same type are added consecutively. This can be achieved with a single pass through the sequence, resulting in a linear time complexity.

Brute Force Approach

The brute-force approach would involve iterating through the sequence for each operation to validate it against all previous operations, resulting in a time complexity of O(n²). This naive approach would be highly inefficient for large sequences. It can be implemented using nested loops to compare each operation with all previous ones.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function validateCrateSequence(sequence) {
  let stack = []; 
  for (let i = 0; i < sequence.length; i++) {
    if (sequence[i][0] === 'add') {
      if (stack.length > 0 && stack[stack.length - 1] === sequence[i][1]) {
        return false;
      }
      stack.push(sequence[i][1]);
    } else if (sequence[i][0] === 'remove') {
      if (stack.length === 0) {
        return false;
      }
      if (stack[stack.length - 1] === sequence[i][1]) {
        stack.pop();
      } else {
        return false;
      }
    }
  }
  return stack.length === 0;
}

Asked in Top Tech Interviews

Salesforce

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.