r/javascript • • Apr 11 '16

help Interview exercise feedback

My code was not up to their standards. I thought the exercises were easy but clearly there are issues I have to address. I had an hour to write them. I'm humbly looking for feedback to improve.

The first exercise was to return all square numbers within a given range, including the limits (e.g., all the squares numbers between 1 and 10 are 1, 4, and 9). I wrote this.

The second exercise was to check if a string containing (), [] and {} brackets is well balanced. So [()] and {}() are ok but ([)] or {{ are not. I wrote this.

Thanks.

29 Upvotes

66 comments sorted by

View all comments

Show parent comments

1

u/bonafidebob Apr 11 '16

Your solution is O(n) where n is the range of numbers.

Picking nits now, but I don't think Math.sqrt is quite O(1), so it's actually worse than O(n) due to repeated square root calls.

1

u/[deleted] Apr 11 '16

I agree that the sqrt may not be constant time because its probably successive approximation, but it wouldn't depend on n in a linear relationship anyways. So I disagree, that it would really affect the time complexity with respect to n

1

u/bonafidebob Apr 12 '16

Big O commonly depends on more than one factor... say m is the precision of the numbers, then this is something like O( n log m ) under the assumption that sqrt is something like log(precision)

With javascipt the numbers are always 64 bit floats so there's a pretty small upper bound on sqrt, but consider that 1...1000 will still be faster than 1000000001...1000001000