This command aims to retrieve information from a social graph.
A JSON file with the following structure:
[
{
"id": 1,
"firstName": "Paul",
"surname": "Crowe",
"age": 28,
"gender": "male",
"friends": [
2
]
},
...
]
Link between friends consist in an adjacency list of ids, defined with the friends
attribute.
The solution must allow to retrieve the following information:
- Direct friends: Return those people who are directly connected to the chosen person.
- Friends of friends: Return those who are two steps away, but not directly connected to the chosen person.
- Suggested friends: Return people in the group who know 2 or more direct friends of the chosen person, but are not directly connected to her.
The solution is based on python, built around a command line tool. I focused on providing a solution within 3 hours, without wasting time in managing HTTP request for a REST API or defining a database model, which is not required to solve this problem.
- python, >=2.7
- pip package manager, >=7.0.3
-
for production
pip install https://github.com/zazabe/cargo/archive/master.zip
-
for development
git clone https://github.com/zazabe/cargo.git /opt/source/cargo cd /opt/source/cargo pip install -e .
Notes:
- the code has only been tested on Ubuntu 14.04, but should work on any unix distribution supporting at least python 2.7.
- to run unit tests, you must at least clone the repository and run tests from the root of the repo.
The command support a --help
option for each sub-command.
cargo social <JSON_DATA_FILE> direct <PERSON_ID>
$ cargo social tests/fixtures.json direct 2
2 friend(s) found for Rob Fitz
- Paul Crowe
- Ben O'Carolan
cargo social <JSON_DATA_FILE> steps <PERSON_ID> --steps [NB_STEPS] (default: 2)
$ cargo social tests/fixtures.json steps 2
3 relation with 2 steps for Rob Fitz
- Victor
- Peter Mac
- Sarah Lane
cargo social <JSON_DATA_FILE> suggest <PERSON_ID>
$ cargo social tests/fixtures.json suggest 5
1 friend(s) suggestion for Peter Mac
- Katy Couch
Unit tests are written with py.test, you must clone the source code to have all required files (fixtures, etc...) and run test properly.
Run all tests (at the root of the repository folder):
$ py.test tests/social.py
Additionally, tests are run by Travis CI at every commit.