Skip to content

Instantly share code, notes, and snippets.

@scottoasis
Created July 27, 2015 10:18
Show Gist options
  • Save scottoasis/5d8662201c835dd9718d to your computer and use it in GitHub Desktop.
Save scottoasis/5d8662201c835dd9718d to your computer and use it in GitHub Desktop.
Fill gap interview quiz in ruby.
#!/usr/bin/env ruby
#
#
# - Water Filling Problem -
#
# Imagine we have several columns of bricks forming a
# wall-like structure just like this,:
#
# +-+ +-+ +-+ +-+
# +-+ +-+-+-+ +-+ +-+-+ +-+-+
# +-+ +-+-+-+ +-+-+-+-+ +-+-+-+
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# ----------------------------------- Ground
# fig. 1
#
# into which we can pour water.
#
# Now given an array of how high each column is, could
# you calculate how many water we can pour into gaps of
# this structure?
#
# For example, the structure above could be represented
# with such an array:
# [4,1,3,3,4,1,3,2,3,4,1,1,2,3,4]
#
# and of course here the answer would be 21.
#
# You should implement the fill_gap() function, which
# takes in an array representing bricks, and gives out
# how many water we can pour in it.
#
# This quest comes with a simple test suite, so that you
# can easily check your answer just by execting this file.
#
#
def fill_gap(bricks)
-1
end
# WARN: Don't touch things below me.
def expect(reality, expectation)
if (reality != expectation)
puts ("[FAIL] expect #{expectation} while got #{reality}")
else
puts ("[PASS]")
end
end
expect(
fill_gap([1,3]),
0);
expect(
fill_gap([1, 0, 1]),
1);
expect(
fill_gap([3, 0, 2]),
2);
expect(
fill_gap([4,1,3,3,4,1,3,2,3,4,1,1,2,3,4]),
21);
expect(
fill_gap([4,5,2,3,3,2,3,3,1,5,3,3,3,4]),
22);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment