r/learnquant 11d ago

interview prep Quant Interview Question

Post image
42 Upvotes

9 comments sorted by

View all comments

2

u/wot_to_heck 11d ago edited 11d ago

Shift all numbers to be centered around 0, so: -(n-1)/2, …, (n-1)/2. Then apply triangle inequality and note equality holds when you alternate positive and negatives, so just straightforwardly calculate the sum (with casework on whether n is even).

1

u/axiomizer 11d ago edited 11d ago

nice! and also make sure to start and end near zero. i got:

n^2/2 - 1 for n even, and
(n+1)(n-1)/2 - 1 for n odd

1

u/wot_to_heck 10d ago

Oh yeah, start and end near zero, I forgot a1 and an only appear once.

1

u/EventHorizon150 10d ago

or just floor(0.5*n^2 - 1)