4 ms·
> but what's a better alternative? "Write a function that, when given an array of length n + 1 containing integers 1 through n, find the duplicate integer in a
by jasonlotito 4y ago
> but what's a better alternative?
"Write a function that, when given an array of length n + 1 containing integers 1 through n, find the duplicate integer in an array.
You can use this equation: sum(1 to n) = n(n + 1) / 2"
Basically, if your solution is made trivial by someone knowing a generic formula or equation, just give it to them.
Not giving them the formula doesn't give you any additional information.
- pc86 4y agoOf course it does. Have they seen the question before? Are they really good with math? Are they really bad with math? Do they even know how to approach a mathematical problem like this if they've never seen the formula before? Do they even consider that there might be a mathematical formula to determine this, or do they immediately just try to brute force it? We can debate whether or not any of the above is useful information (I think it is), but not giving them the formula definitely gives you more information than just giving it to them.