Skip to main content
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.7k
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
  • 6,389
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
  • 88.3k
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

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 written ...
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
  • 463
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
  • 58.7k
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
  • 12.2k
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,561
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
  • 67.2k
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
  • 11.6k
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,591
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
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: ...
Loki Astari's user avatar
  • 97.7k
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.7k
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,705
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.9k
10 votes
Accepted

naive C++ stack template class

Design review So, basically, you would like to remake std::stack. That’s cool, it’s not a bad learning project at all. It is actually possible to make a much better ...
indi's user avatar
  • 16.5k
9 votes
Accepted

Python3 Stack implementation with List built-in

By far the biggest problem this code has is error handling. When popping or peeking from an empty stack this returns a string. this is really broken. For an example ...
Oscar Smith's user avatar
  • 3,717
9 votes
Accepted

C++ Updated Stack Code

#include <stdexcept> And either <utility> or <algorithm>, for ...
Quuxplusone's user avatar
  • 19.7k
9 votes

HackerRank Implement Queue using two stacks Solution

You might want to look at the discussion tab for why the code is timing out. Basically the code should reverse the input stack only when peek() or ...
pacmaninbw's user avatar
  • 26.1k

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