Created
June 14, 2021 02:16
-
-
Save colindean/6dec1f1f0de04db2ac0d5d1ed03cec6b to your computer and use it in GitHub Desktop.
Some things I did for an interview once
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
# require "rspec/autorun" | |
# require "minitest/autorun" | |
# You're running a pool of servers where the servers are numbered sequentially starting from 1. Over time, any given server might explode, in which case its server number is made available for reuse. When a new server is launched, it should be given the lowest available number. | |
# Write a function which, given the list of currently allocated server numbers, returns the number of the next server to allocate. | |
# For example: | |
# >> next_server_number([5, 3, 1]) | |
# 2 | |
# >> next_server_number([5, 4, 1, 2]) | |
# 3 | |
# >> next_server_number([3, 2, 1]) | |
# 4 | |
# >> next_server_number([2, 3]) | |
# 1 | |
# >> next_server_number([]) | |
# 1 | |
# Server names consist of an alphabetic host type (e.g. "apibox") concatenated with the server number, with server numbers allocated as before (so "apibox1", "apibox2", etc. are valid hostnames). | |
# Write a name tracking class with two operations, allocate(host_type) and deallocate(hostname). The former should reserve and return the next available hostname, while the latter should release that hostname back into the pool. | |
# For example: | |
# >> tracker = Tracker.new | |
# >> tracker.allocate('apibox') | |
# "apibox1" | |
# >> tracker.allocate('apibox') | |
# "apibox2" | |
# >> tracker.deallocate('apibox1') | |
# nil | |
# >> tracker.allocate('apibox') | |
# "apibox1" | |
# >> tracker.allocate('sitebox') | |
# "sitebox1" | |
def next_server_number(arry) | |
(1..).find do |index| | |
!arry.include? index | |
end | |
end | |
class Tracker | |
# {hosttype -> [number]} | |
def initialize | |
@allocated_hosttypes = {} | |
end | |
def allocate(hosttype) | |
if @allocated_hosttypes.include? hosttype | |
next_number = next_server_number(@allocated_hosttypes[hosttype]) | |
@allocated_hosttypes[hosttype] << next_number | |
else | |
@allocated_hosttypes[hosttype] = [1] | |
next_number = 1 | |
end | |
"#{hosttype}#{next_number}" | |
end | |
def deallocate(hostname) | |
hosttype, number = hostname.match(/([a-z]*)(\d*)/).captures | |
puts @allocated_hosttypes | |
if @allocated_hosttypes.include? hosttype | |
puts @allocated_hosttypes[hosttype] | |
@allocated_hosttypes[hosttype].delete number.to_i | |
end | |
puts @allocated_hosttypes | |
end | |
end | |
tracker = Tracker.new | |
puts tracker.allocate('apibox') | |
puts tracker.allocate('apibox') | |
puts tracker.deallocate('apibox1') | |
puts tracker.allocate('apibox') | |
puts tracker.allocate('sitebox') | |
require "rspec/autorun" | |
# plate weights (in lbs) needed to reach the target weight. The array result may either represent the plate weights needed on one side of the barbell or both sides. | |
# | |
# Weights used to reach the target weight will be in the form of the barbell and plates of specific weights. | |
# | |
# Rules: | |
# - The barbell weighs 45 lbs. | |
# - The barbell is always needed, so the minimum valid weight is 45 lbs. | |
# - Plates are weighted as 45 lbs, 35 lbs, 25 lbs, 10 lbs, and 5 lbs. | |
# - Any number of plate weights of a given size may be used to reach the target weight; there are no limits on the number of plate weights available for use. | |
# - The weights must be equal on both sides of the barbell. You cannot have a 45 lbs plate on one end and only a 25 lbs plate on the other. | |
# - The maximum valid weight is 405 lbs. Anything above this would not fit on the barbell. | |
# - You must use the heaviest plates available. To represent 55 lbs on one side, you must use the plates (45 lbs + 10 lbs). Both (25 lbs + 25 lbs + 5 lbs) and (35 lbs + 10 lbs + 10 lbs) are invalid. | |
# - Any input that cannot be represented is considered invalid. Invalid cases may be handled by using either the nearest valid weight or no weight. | |
# | |
# | |
# Test Cases | |
# | |
# > plate_weights(45) | |
# [] | |
# | |
# > plate_weights(135) | |
# [45] | |
# | |
# > plate_weights(405) | |
# [45, 45, 45, 45] | |
# | |
# > plate_weights(175) | |
# [45, 10, 10] | |
# | |
# > plate_weights(105) | |
# [25, 5] | |
# | |
# > plate_weights(355) | |
# [45, 45, 45, 10, 10] | |
# | |
# > plate_weights(215) | |
# [45, 35, 5] | |
BAR_WEIGHT = 45 | |
MAX_WEIGHT = 405 | |
def number_of_weights_at_weight(target, weight) | |
number = target / weight | |
remaining = target % weight | |
[number, remaining] | |
end | |
def plate_weights target_weight | |
return [] if target_weight < BAR_WEIGHT | |
return [] if target_weight > MAX_WEIGHT | |
target_weight_without_bar = target_weight - BAR_WEIGHT | |
one_side = target_weight_without_bar / 2 | |
output = [] | |
[45, 35, 25, 10, 5].reduce(one_side) do |acc, plate| | |
number, remaining = number_of_weights_at_weight(acc, plate) | |
output += Array.new(number) { |i| plate } | |
remaining | |
end | |
output | |
end | |
RSpec.describe do | |
context "has no weights" do | |
it "when target under 45" do | |
expect(plate_weights(44)).to eq([]) | |
end | |
it "when target over 405" do | |
expect(plate_weights(406)).to eq([]) | |
end | |
end | |
examples = { | |
405 => [45, 45, 45, 45], | |
135 => [45], | |
175 => [45, 10, 10], | |
105 => [25, 5], | |
355 => [45, 45, 45, 10, 10], | |
215 => [45, 35, 5], | |
237 => [] | |
} | |
examples.each_pair do |k,v| | |
it "#{k} produces #{v}" do | |
expect(plate_weights(k)).to eq(v) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment