Hot answers tagged

34 votes
Accepted

Balanced parentheses

This is almost all style suggestions; the code itself looks great. Personally, I prefer the brace-on-same-line style for everything in JS, and I prefer proper blocks instead of inlining expressions. ...
user avatar
  • 32.9k
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. ...
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 ...
user avatar
28 votes
Accepted

Stack implementation using Swift

Let's start with some general remarks: Swift does not require semicolons after statements (but they are allowed). From what I have seen since Swift was introduced last year, most people do not write ...
user avatar
  • 22.2k
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 ...
user avatar
  • 18.8k
23 votes
Accepted

Implementing a Stack with Templates and Smart pointers

Readability Please put a space after the include and before the < ...
user avatar
  • 91.5k
21 votes
Accepted

Reversing a String using Stack

Unicode is hard to get right, especially in Java as it has a more or less broken concept of a “character”. An Unicode code point is a 21-bit number. These code points are encoded to bytes with the UTF-...
user avatar
  • 12.4k
20 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 ...
user avatar
  • 3,493
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 ...
user avatar
  • 57.6k
18 votes
Accepted

Implementing a Stack in Java for a technical interview

A stack should not know about the first element. A stack only know about the last element that was pushed, so the Stack object itself should only have one ...
user avatar
18 votes
Accepted

Stack implementation using a linked list

Time complexity The time complexity of push and pop operations should be \$O(1)\$, and so it is in your case too. It doesn't matter how many elements you have, these operations should take constant ...
user avatar
  • 107k
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), ...
user avatar
  • 281
17 votes

Reversing a String using Stack

This question provides a really good example of how learning 'tools' is important, but understanding efficiency is better. The purpose of the task was probably to teach you to use a stack, but there ...
user avatar
  • 96.7k
16 votes
Accepted

Check for balanced parentheses

I just wanted to write that the code looks completely fine to me, until... assertEquals(false, isBalanced("[[")); I''m sure you'll find the error :-) ...
user avatar
  • 10.3k
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 ...
user avatar
  • 541
15 votes

Implementation of stack using pointers

Please fix your indentation all over the header. It should be consistent with everything else. You don't need the return 0 at the end of ...
user avatar
  • 34.7k
14 votes
Accepted

Stack implementation using an array

sscanf is not safe It's not part of your stack but part of your driver code, but scanf() is not a good function to use. There are many reasons for this, but the ...
user avatar
  • 64.1k
14 votes
Accepted

Implementation of stack using pointers

First of all, I will start with one of the most common remarks: please, do not use using namespace std;. It is especially bad if you write it in a header file since ...
user avatar
  • 19.8k
14 votes

Implementing a Stack in Java for a technical interview

As usual, revewing a design rather than code. As coded, pop takes time proportional to the current stack size. It could and should take constant time. Hint: ...
user avatar
  • 52.9k
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 ...
user avatar
  • 64.1k
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 ...
user avatar
14 votes

Array-based stack

The methods named emptyCheck() and fullCheck() are ambiguous. They should be named isEmpty() ...
user avatar
  • 453
14 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 ...
user avatar
13 votes

Check for balanced parentheses

assertEquals vs assertTrue I see that you're using : ...
user avatar
  • 6,661
13 votes

Reversing a String using Stack

@amon wrote a great answer. With Java 8, handling of surrogate pairs is even easier. There is a codePoints() method on the CharSequence interface. ...
user avatar
  • 541
13 votes
Accepted

Java and C++ stack implementations with dramatically different performance

Performance I'll get to a bit of a review, but first, since you're most interested in performance, I've done some benchmarking. In short, all evidence points towards maaartinus' theory being the ...
user avatar
  • 10.5k
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 ...
user avatar
  • 43.8k
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 ...
user avatar
  • 52.9k
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 ...
user avatar

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