Stack Intro - Getting Started / Basic Data Structures

in python stack[-1] means counting from the top of the stack by 1 pointer
similarly stack[-2] is counting from 2 pointer from the top

Using the platorm for the first time. When I submit the given test cases as custom input, I get following error.

Traceback (most recent call last):
  File "", line 1, in <module>
    def execute(program: List[str]) -> List[int]:
NameError: name 'List' is not defined

from typing import List

Popping an empty stack will kick out an IndexError exception. The code should check for the empty list before popping. The tests should also check for that error. This input for example crashes:

push 3
pop ← here I added an extra pop

That is, an item can be inserted and removed from a stack, but an item can only be removed from the stack after all the items are added after it is removed first.

The wording for this is confusion, maybe something like: That is, an item can only every be inserted and removed from the top of the stack. Therefore, the first element can only ever be removed once all other elements, added afterwards, have been removed.

My first day for renew the Stack DS.

The numbers in Test #1 are in a wrong order. 0 should be before 7 becouse it was inserted to the stack last.

I think it is correct:
push 3 → stack: [3]
push 7 → stack: [3, 7]
push 20 → stack: [3, 7, 20]
peek → 20
pop → stack: [3, 7]
push 0 → stack: [3, 7, 0]
push 4 → stack: [3, 7, 0, 4]
pop → stack: [3, 7, 0]

Since 0 was inserted last, it is at the end of the array (stack).

1 Like

What happened to the 8 at the beginning of the stack. Should it not be: 8 push 3 → stack: [8,3]

Although you might still want to reset it for languages with garbage collectors to prevent memory leaks
Probably there’s a error in phrasing and the author meant “without” instead of “with”

Is the ‘8’ suppose to be the first element of the program?

I don’t understand why the templates are part of this Stack Intro learning module. Does someone know why they are here?

the templates dropdown shows all the templates for each copy pasting when solving problems, not directly related to this problems :slight_smile:

8 is number of inputs

Any chance to get Kotlin as implementation language?

No, ‘8’ is used to further ask the user to input 8 times in line 21, so that a list with 8 string items can form.

For practice stacks try solve the

  1. Valid Parentheses

from leetcode

Wouldn’t it be more efficient to implement the stack with a linked list instead of an array list? at least in Java. An ArrayList will have to instantiate a new array and copy the elements over every time it reaches its internal array’s max capacity.

1 Like

Agreed, i had same thought, we just add new element to head and pop it out easily in O(1).

But there will be a problem if u want to access an element using Index. It realltly depends on what usecase. If u are just peek or popping then linkedList seems ideal

But if u are doing a fetch based on index then array is ideal