Repeat Code With LeetCode — Valid Parentheses, But With JavaScript

Curt Corginia
5 min readJul 7, 2023

If you thought solving it one time was not enough fun, wait until you see it get solved a second time!

Photo by Apaha Spi on Unsplash. If you look closely, you will find sideways parentheses!!

I spent a long time browsing InterviewNoodle in search of the blogger who said LeetCode is a great resource for practicing multiple programming languages, but I never found it. In the process, though, I stumbled on a couple articles I really liked.

It has been quite a while since I interviewed anywhere, but the process can be rather discouraging. The second blogger, who states that he has conducted numerous interviews himself, says:

  • The process is imperfect. He is actually a fan of LeetCode, but candidates face a variety of factors, from their mental state, to the current mood of the interviewer, to the simple fact that some candidates get unlucky and receive questions they do not know how to answer, in spite of being competent in their fields
  • Sometimes there are multiple people who are qualified, and the company simply selects the one who seems most qualified. This sounds obvious, but the author points it out to remind us that a rejection is not the company telling us we are not good enough

…and the first blogger writes:

A hard truth I have learned is that as much as you prep and really want something, things don’t always work. Interviewers are rude or in a bad mood. You get a complete curveball question. You get nervous. I think often we forget although we are being interviewed as candidates, you should be assessing the company as well. How did they treat you and make you feel throughout the process? Do your values align with theirs?
Source

I can certainly identify with getting a “rude interviewer.” I myself encountered a very rude interviewer, though that is just my side of the story and for all I know, he is currently blogging about his “entitled interviewee.” While searching his LinkedIn I learned that he has since been promoted from manager to director at a major company — I need to acknowledge that maybe, just maybe, I gave him a bad demonstration.

The Problem

Valid Parentheses

Difficulty: Easy

Skill required: Stacks

Description: Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.
  3. Every close bracket has a corresponding open bracket of the same type.

The JavaScript Angle

I already covered this problem in C++, but there are a couple of mental leaps. First of all, a Stack is the perfect data structure. If you receive an opening character, push to the stack. If you receive a closing character, pop from the stack. Verify that the character you popped off the stack matches. If it does, keep processing. If it does not, you do not have a valid set of matching parentheses and should return “false/invalid” on the entire string.

The second mental leap is that you need a way to map the values. Two weeks ago I did this with a function, but you can easily achieve the same thing with a hashmap. A ( maps to ), a [ maps to ], a { maps to }.

For your entertainment, I am going to show you my first accepted LeetCode JavaScript solution.

Why did I bring up the “rude interviewer” story? Was it just my excuse to vent and rage against a person I am not identifying, at a company I am not identifying? Well…yes…but it is also a story that demonstrates the importance of programming language. In LeetCode, you can just switch language. In a real interview, like that one, the interviewer may require you to pick one language. That is what happened there. I could not use C++ and really stumbled.

First thing: This is a map in JavaScript. To add key-value pairs to the map, you can use the set function. To retrieve values by key, you can use the get function. Why my frustrated console.log statements? You can use get(), but you cannot simply retrieve values using square brackets, the way you might in C++.

The second set of console.log statements were me working through another bug. I was trying to go from value to key instead of key to value, which is technically possible, but less efficient…and arguably defeats the purpose of using a map in that manner.

A bit confusing for me — JavaScript does not have a data structure explicitly called the stack, but it has arrays and arrays have push and pop methods that behave exactly like those of a stack. The array does not have a method called top(), meaning I used the rather confusing s1[s1.length-1] to retrieve the top.

This, at least in my opinion, is counterintuitive. You can read more about it on StackOverflow here, but a stack is first-in, last-out and you want the last element in the array. So you can use array[array.length-1] to get top, and not array[0].

Here is the code again, cleaned up:

Note that line 26 was changed to ===. The use of == there was a mistake.

There is some debate over == vs === on StackExchange. Some say that == is never good, and others argue that only a Sith believes in absolutes, but I used == simply out of habit.

Let, var, and const are explained here. This is a big topic and is worth revisiting. For now, I hope I made the correct decisions in let vs. const vs. var (I did not use var here at all), but to some extent it is a matter of preference and there is some debate on StackOverflow over which is most error-prone.

Closing Thoughts

This could take some getting used to.

--

--

Curt Corginia

Founder, CEO, CTO, COO, and janitor at a company I made up called CORGICorporation