Code Monkey home page Code Monkey logo

frontend-interview-questions's Issues

assignDeep function fails when the object value contains date object.

As in, https://github.com/bcherny/frontend-interview-questions/blob/master/coding-intermediate/assignDeep.js

if you try to call it with let s = assignDeep({a:1}, {b: new Date()}), you get an object: {a:1, b: {}}, rather than {a:1, b: date object}.

The reason lies in:

function isObject(a) { return typeof a === 'object' && a !== null}

if you try to call it as: isObject(new Date()), it returns true, therefore it going to be treated the date as an iterable object, I mean, it recursively call the assignDeep method on the date object.

however, during my tests, function isObject(a) { return typeof a === 'object' && a !== null&& Object.keys(a).length>0 , can be a good solution to check if this source[key] is an interable object.

"isPrime" implementation is wrong

Hello,
Thanks for putting this together, I was just browsing and noticed a bug in implementation of isPrime. It returns wrong results.

Try:

isPrime(9); // -> true
isPrime(21); // -> true
isPrime(25); // -> true

And none of these is prime. It will return true for any odd number, as you are skipping all of the odd number dividers (counter has a value of 2 + (n * 2)) on the line #17

  for (let i = 2; i < Math.ceil(Math.sqrt(n)); i += 2) {
    if (n % i === 0) {
      return false
    }
  }

You just need to increment by 1 not by 2.

Cheers!

BinarySearchTree: wrong size() in example

Original question

let tree = new BinarySearchTree
tree.add(1, 2, 3, 4)
tree.add(5)
tree.has(2)                           // true
tree.has(5)                           // true
tree.remove(3)                        // undefined
tree.size()                           // 3

Should be tree.size() // 4 (total number of nodes)

Or maybe tree.depth() // 3 (highest distance from root, in case it is not self-balancing BST)

reverse: wont work with Unicode surrogate pairs

Current implementation

function reverse(string) {
  let index = string.length - 1
  let result = ''
  while (index > -1) {
    result += string[index]
    index--
  }
  return result
}

same as this one (short and popular one)

function reverse(string) {
    return string.split('').reverse().join('');
}

Both wont work with with Unicode chars which length is more than 16 bit (surrogate pairs).
Because both split('') and string[i] will address characters by 16 bit blocks, and so destroy Unicode pairs.

Ex.:

reverse('abc ๐Ÿš€') // Error: expected '๏ฟฝ๏ฟฝ cba' to equal '๐Ÿš€ cba'

Solution is es6 string[Symbol.iterator] via destructuring, which takes Unicode chars into account (proof):

function reverse(str) {
  return [...str].reverse().join('');
}

Probably you want to keep answers as transparent as possible and split().reverse().join() is too hack-ish for educational purposes. But maybe you can mention Unicode issue in the comment above the solution for people to know. Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.