DEV Community

Valid parentheses, solving a Facebook interview question.

Akhil on June 27, 2020

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 ...
Collapse
 
namhle profile image
Nam Hoang Le • Edited

You can check a number is not equal to zero by if (n) {} or check empty string by if (!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.

Collapse
 
akhilpokle profile image
Akhil

I wrote this way for code readability :)

Collapse
 
namhle profile image
Nam Hoang Le • Edited

Yes, that way is better for products. But the boundary between readability and cleaning is not always clear. :)

Thread Thread
 
akhilpokle profile image
Akhil

True that.

Collapse
 
js_bits_bill profile image
JS Bits Bill

Also tried the standard for loop approach which actually proved easier to deal with than every:

const isValid = function(s) {

  const pairs = {
    '(': ')',
    '[': ']',
    '{': '}'
  };

  const openParens = [];
  for (let i = 0; i < s.length; i++) {
    const endParen = pairs[s[i]];

    if (endParen) openParens.push(endParen);
    else if (openParens[openParens.length - 1] === s[i]) openParens.pop();
    else return false;
  } 

  return openParens.length ? false : true;
};
Collapse
 
akhilpokle profile image
Akhil

Awesome work man ! keep up !

Collapse
 
namhle profile image
Nam Hoang Le

It’s me again. Here’s my solution.

function isValid(s) {
    const t = s.replace(/\(\)|\[\]|\{\}/g, "");
    return s == t ? !s : isValid(t);
};
Collapse
 
akhilpokle profile image
Akhil

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 !

Collapse
 
namhle profile image
Nam Hoang Le • Edited

It’s true. C/C++ can solve it in almost 0ms.

Collapse
 
js_bits_bill profile image
JS Bits Bill • Edited

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 a for...of loop which works nicely with the empty string case.

const isValid = function(str) {

  const pairs = {
    '(': ')',
    '[': ']',
    '{': '}'
  };

  const closingParens = [];
  return [...str].every(validate);

  function validate(char, i, arr) {
    const isLastChar = i === arr.length - 1;

    // If char found in pairs, push closing paren to arr
    if (pairs[char] && !isLastChar) {
      closingParens.push(pairs[char]);
      return true;

    } else {
      // If char has no closing paren, return false
      if (!closingParens.length) return false;

      // Check if current char is last in arr
      const lastCharInArray = closingParens.pop();

      // If last char and unmatched parens, return false
      if (isLastChar && closingParens.length) return false;

      // If pair found, continue
      if (char === lastCharInArray) return true;
    }
  }
};

What do you think?

Collapse
 
akhilpokle profile image
Akhil

For :"{[]}" it returns false.

Try solving it here : leetcode.com/problems/valid-parent...

Collapse
 
js_bits_bill profile image
JS Bits Bill

@akhil I updated it to meet that condition!

Collapse
 
vladflorescu94 profile image
vladflorescu94 • Edited

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;
  • variables should have more descriptive names and start with a lowercase character;
  • always ===, never ==;
  • static constants should be defined outside functions;
  • renturn stack.length === 0 is cleaner.

I wrote the implementation below and I find it much cleaner.

const CLOSING_BRACKET_BY_MATCH = {
  '(': ')',
  '[': ']',
  '{': '}',
};

const testBrackets = (input) => {
  const stack = [];
  let len = 0;

  if (input.length % 2 !== 0) {
    return false;
  }

  for (const character of input) {
    if ('([{'.indexOf(character) !== -1) {
      stack.push(character);
      len += 1;
    } else {
      const lastCharacterPushed = stack[len - 1];

      if (len === 0 || (CLOSING_BRACKET_BY_MATCH[lastCharacterPushed] !== character)) {
        return false;
      }

      stack.pop();
      len -= 1;
    }
  }

  return len === 0;
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mayankjoshi profile image
mayank joshi

Amazing post. I loved the concept.🥳

Just one thing, try moving this from webdev to #algorithms tag for better reach among algo geeks.

Collapse
 
akhilpokle profile image
Akhil

My bad, it was meant to be #tutorial.

Thanks a lot for reading :)