Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[RC Diary] Interviews back to back (-27)

[RC Diary] Interviews back to back (-27)

In the first interview I got asked the balance parens question:

You have a string made up of just closed and opened parens, write a function that given that string returns true if parens are balanced, false otherwise

This was my approach outside the interview world:

const assert = require('assert')

Array.prototype.pop = Array.prototype.shift
Array.prototype.push = Array.prototype.unshift

let balanced = code => {
  let parenthesis = code.split(''),
      stack = [parenthesis.pop()]

  parenthesis.forEach((current) => {
    let top = stack.pop()
    if (!top) {
      stack.push(current)
    } else if (top !== '(' || current !== ')') {
      stack.push(top)
      stack.push(current)
    }
  })
  return stack
}

assert.equal(balanced('()').length, 0)
assert.equal(balanced('((').length, 2)
assert(balanced('(()))()').length, 1)
assert.equal(balanced('(())').length, 0)

I liked how I remembered to extend Array with correct named functions, something I didn't do in the interview, and, of course, led to confusion because come on, shift and unshift? Really?

During the interview I tried to remember the steps to arrive to the solution, and I ended up with an incomplete version of the above, so actually preparing led to a bad situation. Or probably it was just the pression of having solved this and not really being able to solve it again, which a new thing for me. I've never got to the point where I was this prepared for interviews so probably I now have to figure out how to deal when I heard about a problem solution but I can't remember it.

Anyway, thile this is what I ended up with during the interview after some hints

function balance(string) {
  let counter = 0,
      inputAsArray = string.split('');
  
  for (let i = 0; i < inputAsArray.length; i++) {
    let current = inputAsArray[i];
    if (current === '(') counter++;
    if (current === ')') counter--;

    if (counter < 0) return false;
  }

  return counter === 0;
}


assert.equal(balance('()()'), true)
assert.equal(balance('(()'), false)
assert.equal(balance(')))'), false)
assert.equal(balance('((()))'), true)

It looks like a much cleaner impelementation for the case at hand, I think it would break if we introduce [ and ], for example in ([)].

Generally speaking I think my performance on the first question affected my feelings about the whole interview process, even though I genuinely think I didn't perform that great, I found that now, after so many Fridays spent doing mock interviews on a whiteboard, if I don't have one I feel a sense of uneasiness.

Another question I got was one about promises, sadly I've lost the code in the jsfiddle but I still have a good article about best practices to read about related concepts and a bit more to study about promises in general as I seem to be missing he key ideas behind them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment