Skip to main content
Share Your Experience: Take the 2024 Developer Survey
33 votes

Stack data structure in Python 3

From a pure Python standpoint, a list is already a stack if you narrow its methods down to only pop and append. ...
Right leg's user avatar
  • 441
31 votes

A counterexample in balanced parentheses

May I suggest a simpler solution: You need only a counter. it is incremented by 1 whenever you encounter an openning parenthesis "(" and is decreased by 1 whenever you encounter a closing parenthesis ...
Sharon Ben Asher's user avatar
25 votes
Accepted

HackerRank Implement Queue using two stacks Solution

Algorithmic complexity for combined operations Containers are interesting elements in most programming languages: they have an internal state, namely their elements and therefore their size. This ...
Zeta's user avatar
  • 19.3k
23 votes
Accepted

Implementing a Stack with Templates and Smart pointers

Readability Please put a space after the include and before the < ...
Martin York's user avatar
  • 95.2k
21 votes
Accepted

Simple Stack implementation in C++

Your stack implementation is terrible, and so is @hc_dev: neither handles memory correctly. Resource Handling It is generally frowned upon to call new and ...
Matthieu M.'s user avatar
  • 5,137
19 votes

Balanced parentheses using standard C++

Avoid using namespace std; - it's a large namespace, and growing as new C++ standards appear. Bringing all its names into the global namespace completely obviates ...
Toby Speight's user avatar
  • 75.8k
18 votes

Stack class in Java 8

Other answers correctly point out that using primitive types and not mixing them with Objects (=int instead of Integer), ...
Luke's user avatar
  • 281
16 votes

Leetcode: Valid parentheses

This is a follow up of @Henrik Hansen. Instead, of a switch I would use a Dictionary<T, K>. A Dictionary offers two main advantages: an increase readibility ...
aloisdg's user avatar
  • 561
14 votes
Accepted

Very simple stack in C

I see a number of things that may help you improve your code. Use a stack pointer as a parameter Right now, one can only have one stack at a time. Worse, if one calls ...
Edward's user avatar
  • 66.4k
14 votes
Accepted

Checking for parentheses balance in C

Bugs The loop header is wrong: for(i=0;line[i]!=NULL;i++){ Here, you want to scan until you find a byte with ASCII value 0 (also called an ASCII NUL, and ...
200_success's user avatar
14 votes

Array-based stack

The methods named emptyCheck() and fullCheck() are ambiguous. They should be named isEmpty() ...
TwiN's user avatar
  • 453
13 votes
Accepted

StackList<T> implementation

Issues public void Push(T item) { _stackList.Insert(0, item); } The hurts the stack list. It would be much easier if you just added new items at the end of ...
t3chb0t's user avatar
  • 44.3k
13 votes

Checking for parentheses balance in C

The idea is correct, the implementation is not. Upon exiting the loop you need to test for pilepointer == -1. Otherwise, the strings like ...
vnp's user avatar
  • 56.5k
13 votes

Stack data structure in Python 3

On the whole the code is good, but I would look for three improvements: Information-hiding: I would change self.stack to ...
snakecharmerb's user avatar
13 votes

Stack data structure in Python 3

The code is clean. My major concern with how it looks is that your naming doesn't follow PEP8. Names should be snake_case, not camelCase. So isEmpty should be ...
Carcigenicate's user avatar
12 votes
Accepted

LeetCode parentheses matching

Usage of Stack Don't use a Stack. This class is very old, it's thread safe (which you don't need) and achieves it by ...
Duarte Meneses's user avatar
12 votes

Stack Implementation in C++ with Exceptions

std::allocator_traits is a better way to interact with an allocator. It provides defaults for construct and destroy if the allocator doesn't do it itself. ...
ratchet freak's user avatar
12 votes

Balanced parentheses using standard C++

using namespace std; may seem fine for small projects, but can cause problems later so it's best to avoid it. Prefer to output "\n" instead of ...
user673679's user avatar
  • 11.8k
12 votes

Stack class in Java 8

This looks good, However I suggest you properly indent the code. Further suggestions: I would change following private final int INCREMENTSIZE = 1024; to <...
JaDogg's user avatar
  • 4,531
11 votes
Accepted

Dynamic stack C implementation

So you have build a dynamic size stack containing integers, and you guarantee that the allocated size if a power of 2 and the used size is between the allocated size and its quarter. This automatic ...
Serge Ballesta's user avatar
11 votes

Checking for parentheses balance in C

Here are some things that may help you improve your code. Don't use gets Using gets is not good practice because it can lead ...
Edward's user avatar
  • 66.4k
11 votes

Array-based stack

Formatting You should clean up your indention. The methods should be indented to the same level as the fields and it is common in Java to place the opening bracket on the same line as the matching ...
RoToRa's user avatar
  • 11k
11 votes

Leetcode: Valid parentheses

A couple of small things to add: Maybe not applicable to a timed interview, but inline documentation (///) on public members is always nice, and would help to ...
VisualMelon's user avatar
  • 7,431
11 votes

Stack class in Java 8

Your class is small and does what you said it should, that's good. However, there are a few things you could do better. 1. Use primitive types unless the boxed ones are specifically needed: You are ...
GiantTree's user avatar
  • 628
10 votes

Finding the maximum element of a Stack

This type of question actually requires a little thinking outside the box. The idea here, since we need to store the max with each element, is to use the built-in ...
Der Kommissar's user avatar
10 votes
Accepted

Stack Implementation in C++ with Exceptions

I would make methods names start with a lower case letter to distinguish them from user defined types. But that's me. A couple of small things: ...
Martin York's user avatar
  • 95.2k
10 votes

Validator for mathematical expression in infix form

Don’t write using namespace std;. You can, however, in a CPP file (not H file), or inside a function, put individual ...
JDługosz's user avatar
  • 11.4k
10 votes
Accepted

C pointer based growable stack

I suggest that you do more error checking: malloc or realloc may fail. I can initialize the stack with negative capacity. I can ...
L. F.'s user avatar
  • 9,560
10 votes

Stack Implementation in JavaScript

Encapsulate The object Stack should protect its state as it is easy to add and remove items via the exposed referenced ...
Blindman67's user avatar
  • 22.1k

Only top scored, non community-wiki answers of a minimum length are eligible