Created
March 1, 2019 20:41
-
-
Save RichardJordan/b3e1686834cddd38ae8213177e01c81a to your computer and use it in GitHub Desktop.
Interview Questions -- 1
This file contains 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
# I liked this interview question. It fit nicely in about an hour, and I | |
# think I'd consider re-using it. It isn't too gimmicky and it's not one | |
# of those "did you do a CS degree" tests, which help nobody. | |
# | |
# Q: given a json input in the form of a set of jobs and subjobs: | |
# | |
# '[ | |
# {"id":"1","name":"Designer","parent_id":null}, | |
# {"id":"2","name":"Engineer","parent_id":null}, | |
# {"id":"3","name":"Software Engineer","parent_id":"2"}, | |
# {"id":"7","name":"UX Designer","parent_id":"1"}, | |
# {"id":"6","name":"UI Designer","parent_id":"1"}, | |
# {"id":"5","name":"FrontEnd Engineer","parent_id":"3"}, | |
# {"id":"4","name":"Backend Engineer","parent_id":"3"} | |
# ]' | |
# | |
# we want an output that shows jobs and subjobs thus: | |
# | |
# Designer | |
# -UI Designer | |
# -UX Designer | |
# Engineer | |
# -Software Engineer | |
# --Backend Engineer | |
# --FrontEnd Engineer | |
# | |
# In the live interview I did this as a set of methods called at the end, | |
# but with a few mins to think about it a quick refactor gives me this | |
# as my solution: | |
module Helpers | |
def sorted(arr) | |
arr.sort { |a,b| a.name <=> b.name } | |
end | |
end | |
class Element | |
include Helpers | |
attr_reader :children, :id, :parent_id, :name | |
def self.from(element_hashes) | |
element_hashes.map do |ele| | |
new(id: ele["id"], parent_id: ele["parent_id"], name: ele["name"]) | |
end | |
end | |
def initialize(id:, parent_id:, name:) | |
@id = id | |
@parent_id = parent_id | |
@name = name | |
@children = [] | |
end | |
def add_child(element) | |
children << element | |
end | |
def claim_children!(array) | |
child_candidates.each do |candidate| | |
add_child if (candidate.parent_id == id) | |
end | |
self | |
end | |
def print(leader="") | |
puts leader + name | |
unless children.empty? | |
sorted(children).each do |child| | |
child.print(leader + "-") | |
end | |
end | |
end | |
end | |
class Tree | |
include Helpers | |
attr_reader :element_array, :element_tree, :roots | |
def self.from(array) | |
new.tap { |t| t.build_from(array) } | |
end | |
def initialize | |
@element_array = [] | |
@element_tree = [] | |
@roots = [] | |
end | |
def build_from(array) | |
@element_array = array | |
claim_children! | |
self | |
end | |
def print | |
return if roots.empty? | |
sorted(roots).each(&:print) | |
end | |
private | |
def claim_children! | |
element_array.each do |element| | |
element.claim_children(element_array - [element]) | |
element_tree << element | |
roots << element if element.parent_id.nil? | |
end | |
end | |
end | |
def print_roles | |
elements = Element.from(@roles) | |
tree = Tree.from(elements) | |
tree.print | |
nil | |
end | |
# $ require 'json' | |
# => true | |
# $ @roles = JSON.parse([json-array-above]) | |
# => [{"id"=>"1", "name"=>"Designer", "parent_id"=>nil}, {"id"=>"2", "name"=>"Engineer", "parent_id"=>nil}, | |
# {"id"=>"3", "name"=>"Software Engineer", "parent_id"=>"2"}, {"id"=>"7", "name"=>"UX Designer", "parent_id"=>"1"}, | |
# {"id"=>"6", "name"=>"UI Designer", "parent_id"=>"1"}, {"id"=>"5", "name"=>"FrontEnd Engineer", "parent_id"=>"3"}, | |
# {"id"=>"4", "name"=>"Backend Engineer", "parent_id"=>"3"}] | |
# $ print_roles | |
# Designer | |
# -UI Designer | |
# -UX Designer | |
# Engineer | |
# -Software Engineer | |
# --Backend Engineer | |
# --FrontEnd Engineer | |
# => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment