Skip to content

Instantly share code, notes, and snippets.

@dougdroper
Created May 30, 2014 09:20
Show Gist options
  • Save dougdroper/e6e4dbbb950038eeb537 to your computer and use it in GitHub Desktop.
Save dougdroper/e6e4dbbb950038eeb537 to your computer and use it in GitHub Desktop.
employee
require 'csv'
class Tree
def initialize(employees)
@managers = employees.group_by {|e| e["Manager"] }
@employee_names = employees.map {|e| e["Employee Name"]}
(@managers.keys.compact - @employee_names).each do |name|
@managers[nil] << {"Employee Name" => name}
end
end
def walk(name=nil,level=0, &block)
@managers.fetch(name, []).each do |employee|
yield employee, level
walk(employee["Employee Name"], level + 1, &block)
end
end
end
employees = CSV.read('sherwood_plc.csv', :headers => true)
Tree.new(employees).walk do |employee, level|
puts ">> " * level + employee["Employee Name"]
end
@dougdroper
Copy link
Author

Your company, Sherwood Plc, is looking to obtain Investor in
People status. To achieve this they need to understand the
current company structure; who reports to who, how much people
are being paid, etc. The HR director has asked the IT team
to use the company HR database to help provide this information.

The task:

You will need to create a short program to read in the data
from the HR database and build up a structured model of the
employees described in it.

The HR database is a CSV file, with one employee per row of the
file and a single header row describing the columns; the employee
name, their line managers name, their department and their salary.

Below is some example data:

Employee Name,Manager,Department,Salary
Robin Hood,,,200
Friar Tuck,Robin Hood,Wealth Redistribution,100
Little John,Robin Hood,Wealth Redistribution,100

In this file, Robin Hood is the manager of both Friar Tuck and
Little John. They all work in the wealth redistribution
department.

N.B. In the data file, there's NO guarantee as to the order the
employees appear in.

What we're looking for:

We're looking for a simple ruby program that takes a file as an
argument and outputs the employee structure. Maybe something like:

$ ruby employee_structure.rb merry_men.csv
Robin Hood (no department)

Friar Tuck (Wealth Distribution)
Little John (Wealth Distribution)
(...and so on...)

Our goal in setting this task is to get a better understanding of
your style of coding and how you write software, so write your
solution with that in mind.

Feel free to use:

  • relevant libraries
  • another language if you feel it will better demonstrate your skills

Here are some more questions to consider if you have time:

  • What happens when a manager isn't listed in the file?
  • What happens if there are extra columns in the data?
  • How hard would it be to change the output to list employees
    by department, or by salary?
  • How should it respond to missing data, e.g. missing employee name?
  • Future features that the HR Director may ask for.

This is the CSV file:

Employee Name,Manager,Department,Salary
Robin Hood,,,200
Friar Tuck,Robin Hood,Wealth Redistribution,100
Little John,Robin Hood,Wealth Redistribution,100
Guy Gisbourne,The Sherrif,Villainy and Acquisitions,130
The Sherrif,Prince John,Villainy and Acquisitions,200
Prince John,,,300
Maid Marion,Robin Hood,Merrie Management,130
Will Scarlett,Maid Marion,Merrie Management,90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment