BackmediumStackPayPal

Cargo Stack Operations Solution

Problem Statement

Given a stack of integers representing cargo shipments and a list of operations, where each operation is either 'import' with a specific cargo quantity or 'export' to remove the top shipment from the stack, determine the final state of the cargo stack after applying these operations. If the export quantity is greater than the stack size or not present in the stack, ignore the operation.

Example 1
Input
[1, 2, 3], ['import', 1], ['import', 2], ['import', 3], ['export', 3], ['export', 3]
Output
[1, 2]

Explanation: Step-by-step: 1. Import 1, 2, and 3 into the stack: [1, 2, 3]. 2. Export 3 from the stack: [1, 2]. 3. Export 3 from the stack again, but since there is no 3 in the stack, we ignore this operation.

Example 2
Input
[1, 2, 3], ['export', 4]
Output
[1, 2, 3]

Explanation: Step-by-step: 1. The export operation with quantity 4 is not present in the stack, so we ignore this operation.

Constraints

  • {"name":"operationTypes","type":"string","description":"Each operation 'type' can be either 'import' or 'export'."}
  • {"name":"quantityRange","type":"integer","description":"The 'quantity' for 'import' operations is a positive integer."}
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

Cargo Stack Operations — Problem Statement & Solution Guide

StackMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

Given a stack of integers representing cargo shipments and a list of operations, where each operation is either 'import' with a specific cargo quantity or 'export' to remove the top shipment from the stack, determine the final state of the cargo stack after applying these operations. If the export quantity is greater than the stack size or not present in the stack, ignore the operation.

Examples

Example 1

Input

[1, 2, 3], ['import', 1], ['import', 2], ['import', 3], ['export', 3], ['export', 3]

Output

[1, 2]

Explanation: Step-by-step: 1. Import 1, 2, and 3 into the stack: [1, 2, 3]. 2. Export 3 from the stack: [1, 2]. 3. Export 3 from the stack again, but since there is no 3 in the stack, we ignore this operation.

Example 2

Input

[1, 2, 3], ['export', 4]

Output

[1, 2, 3]

Explanation: Step-by-step: 1. The export operation with quantity 4 is not present in the stack, so we ignore this operation.

Constraints

  • {"name":"operationTypes","type":"string","description":"Each operation 'type' can be either 'import' or 'export'."}
  • {"name":"quantityRange","type":"integer","description":"The 'quantity' for 'import' operations is a positive integer."}

Optimal Approach & Strategy

An optimized approach utilizes a stack data structure to directly add or remove elements from the top, resulting in a time complexity of O(n) since each operation (import or export) is a constant time operation.

Brute Force Approach

A brute-force approach involves iterating through each operation and manually updating the stack by shifting elements for each import and export, resulting in a time complexity of O(n²) due to the inefficient shifting.

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

PayPal

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.