Last active
November 12, 2018 14:02
-
-
Save dpisarewski/e775ae809a2392879daa5a14e7c9fb56 to your computer and use it in GitHub Desktop.
google interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simulation for question #27 from https://www.inc.com/business-insider/google-hardest-interview-questions.html | |
class Simulation | |
def initialize(drop_rate, sidewalk_size) | |
@drop_rate = drop_rate # drops per second | |
@sidewalk_size = sidewalk_size # size of the sidewalk in drops(100 for 1m sidewalk with 1cm drop size) | |
@sidewalk = Hash.new # use a hash for much quicker lookup compared to a 2-dimentional array | |
end | |
def run | |
i = 0 | |
while !covered? | |
x = rand(0...@sidewalk_size) | |
y = rand(0...@sidewalk_size) | |
@sidewalk[x] ||= Hash.new | |
@sidewalk[x][y] = true | |
i +=1 | |
end | |
i / @drop_rate | |
end | |
def covered? | |
[email protected]? && @sidewalk.all? { |k, v| v.size == @sidewalk_size } | |
end | |
end | |
simulation_runs = 10 | |
simulation_runs.times do | |
seconds = Simulation.new(1, 100).run | |
puts "#{seconds} seconds needed to cover the sidewalk" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment