Skip to content

Instantly share code, notes, and snippets.

@showell
Created April 11, 2011 19:38
Show Gist options
  • Save showell/914151 to your computer and use it in GitHub Desktop.
Save showell/914151 to your computer and use it in GitHub Desktop.
Common Interview Question: longest run of integers (implemented in coffeescript w/tests)
assert = (cond) ->
throw("error") unless cond
max_run = (a) ->
m = 0
c = 0
for n in a
c += n
c = 0 if c < 0
m = c if m < c
m
do () ->
assert(max_run([]) == 0)
assert(max_run([5]) == 5)
assert(max_run([5, 2]) == 7)
assert(max_run([5, 2, -3]) == 7)
assert(max_run([5, 2, -3, 4]) == 8)
assert(max_run([5, 2, -3, 4, -2]) == 8)
assert(max_run([5, 2, -3, 4, -2, 3]) == 9)
assert(max_run([5, 2, -3, 4, -2, 3, -15]) == 9)
assert(max_run([-1000, 5, 2, -3, 4, -2, 3, -15]) == 9)
assert(max_run([2, -1000, 5, 2, -3, 4, -2, 3, -15]) == 9)
assert(max_run([43, -1000, 5, 2, -3, 4, -2, 3, -15]) == 43)
assert(max_run([1001, -1000, 5, 2, -3, 4, -2, 3, -15]) == 1001)
assert(max_run([3, -1000, 4]) == 4)
assert(max_run([500, -1000, 4]) == 500)
assert(max_run([1001, -1000, 4]) == 1001)
assert(max_run([1001, -1, 4]) == 1004)
assert(max_run([-10, 2, 3, -2, 5, -15]) == 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment