This is one of the most commonly asked screeing questions, Especially if the interview is conducted by Hackerrank, there's a good chance that they ...
For further actions, you may consider blocking this person and/or reporting abuse
You can check a number is not equal to zero by
if (n) {}
or check empty string byif (!s) ...
.Of course, just use object literal for you
map
. :)The last statement should be
return !stack.length
.Bonus point: we can solve it shorter by using recursion and String replace method.
I wrote this way for code readability :)
Yes, that way is better for products. But the boundary between readability and cleaning is not always clear. :)
True that.
Also tried the standard
for
loop approach which actually proved easier to deal with thanevery
:Awesome work man ! keep up !
It’s me again. Here’s my solution.
Test it here: leetcode.com/problems/valid-parent...
As far as I know, the regex will manipulate string and string manipulation is a bit time consuming heavy in javascript.
But smart and concise code though !
It’s true. C/C++ can solve it in almost 0ms.
I gave this a shot! Similar to yours, my approach was to split the string and track the expected closing parens in a separate array. I used the
every
method instead of afor...of
loop which works nicely with the empty string case.What do you think?
For :"{[]}" it returns false.
Try solving it here : leetcode.com/problems/valid-parent...
@akhil I updated it to meet that condition!
I conduct interviews (not at Facebook), I don't ask questions about algorithms, but I wouldn't probably hire someone who writes code like that, especially if it's a mid-senior position, because there are too many bad practices:
var
shouldn't be used anymore;===
, never==
;renturn stack.length === 0
is cleaner.I wrote the implementation below and I find it much cleaner.
Amazing post. I loved the concept.🥳
Just one thing, try moving this from webdev to #algorithms tag for better reach among algo geeks.
My bad, it was meant to be #tutorial.
Thanks a lot for reading :)