Skip to content

Instantly share code, notes, and snippets.

@ToniRib
Last active March 26, 2016 17:41
Show Gist options
  • Save ToniRib/8d126eab4732e767f456 to your computer and use it in GitHub Desktop.
Save ToniRib/8d126eab4732e767f456 to your computer and use it in GitHub Desktop.

Part 1

Question 1:

What does MVC stand for and what does each part of MVC do?

  • MVC stands for "Model View Controller."
    • Model: Interacts with the database and knows information about the data itself. Basically it handles and any data and business logic. In true MVC, the model only interacts with the controller.
    • View: Displays information about the model to the user, but does not interact with the model directly. Instead, it interacts with the controller which provides it the necessary data for display.
    • Controller: Interacts with both the model and the view. The controller recieves the requests from the user and determines which action to call to retrieve the appropriate data. Once it gets the data from the model, it sends it to the view to be displayed to the user.

Question 2:

# What does the following code do?
company = Company.new({name: 'Pied Piper'})
company.save
  • The first line company = Company.new({name: 'Pied Piper'}) creates an instance of the Company class and stores the name 'Pied Piper' to the instance variable @name. However, it does not save this into the database. The second line company.save actually saves this record to whatever database is being used. If there were validations on the name attribute, they would be run at the time of attempted save.

Question 3:

// Where is the error in the following code?
({
  baz: 'foobarbaz',
  bar: function(){
    return 'foobar';
  },
  foo: function() {
    console.log(this.baz);
    setTimeout(function() {
      console.log(this.bar());
    }, 100);
  }
}.foo());
  • This problem lies in the line console.log(this.bar()); and its usage of the keyword this. At this point in the code, this no longer refers to the current object, but the entire Window object since this.bar() lies inside of the anonymous function inside of setTimeout. Any code that is executed by setTimeout is run in a separate execution context which sets this to the window variable instead of what you may expect (the current object).

Question 4:

// Given an array of integer values, write a function in javascript that returns an array
// of the duplicate values in that array. So in the array [1, 2, 3, 3, 4, 5, 5, 6] the returned
// array should be [3, 5].
nums = [1, 2, 3, 3, 4, 5, 5, 6];

var getDuplicates = function(array) {
  counts = {};
  duplicates = [];

  array.forEach(function(element) {
    if (counts[element]) {
      counts[element] = counts[element] + 1;
    } else {
      counts[element] = 1
    }
  })

  for (var key in counts) {
    if (counts[key] > 1) {
      duplicates.push(parseInt(key));
    }
  }
  
  return duplicates;
};

getDuplicates(nums);

Part 2

Consider the following models. Assume these models exist in a PostgreSQL database with the ORM ActiveRecord siting on top.

/*
Company
  - id
  - name (string)
  - description (text)
  - logo_url (string)
  - score (int)
  - has many funding rounds

FundingRound
  - id (int)
  - funding_in_usd (float)
  - company_id
*/

Question 5:

When using an ORM what techniques could you use to ensure the fastest possible lookup time of the above models and the relationships between those models?

  • At the database level, Rails defaults to setting up the id columns of both tables to be indexes which enables faster table lookups. However, for this example, I would assume that you may be looking up a company by its name more often than id, so I would also an index to the name attribute of the companies table. Additionally, it enable faster searching of funding rounds, I would make the foreign key company_id an index on the funding_rounds table.
  • Also, to prevent against N+1 queries in the controllers and views, when loading a company's funding rounds I would write the query as Company.find(:id).includes(:funding_rounds). This will load all the funding rounds for a company in one query so that there won't have to be separate queries to the database for each funding round.

Question 6:

Design the 2 following RESTful routes. Include expected input as well as the HTTP response code & JSON response object. Assume there are no security considerations expected in the solution. An example is provided below to give context as to what we are expecting in this question.

// GET /funding_rounds/:id
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Model as JSON, 200 success
//   Input Example: {id: 123}
//   Output Example: {funding_round: { id: 123, funding_in_usd: 123.25, company_id: 321 }}

Routes to design:

// GET /companies
//   Controller Process:
//     - Lookup Model.all (all records in the companies table)
//     - No explicit failure case, will simply return empty array if no companies are in the database
//     - Render list of companies as JSON, 200 success 
//   Input example: (no data necessary for an index action)
//   Output example if no companies in database: []
//   Output example if companies exist: {"companies": [{"id": 1, "name": "KITE", "description": "Connecting brands with startups", "logo_url": "http://kite/logo.png", "score": 9}, {"id": 2, "name": "Slack", "description": "Team communication", "logo_url": "http://slack/logo.png", "score": 7}]} 

// POST /funding_rounds
//   Controller process: 
//     - Attempt creation of new funding round record (FundingRound.create(funding_params))
//     - If record could not be saved (failed validations) return 422 status code (unprocessable entity)
//     - If record can be saved, return 201 status code (created) and render the new Model
//   Input example: {"funding_round": { "id": 10, "funding_in_usd": 144.56, "company_id": 5 }}
//   Output example: {"funding_round": { "id": 10, "funding_in_usd": 144.56, "company_id": 5 }}

// One note: If you were using something like the active_model_serializers gem, you may not get the "funding_round" or "companies" part of the output.

Question 7:

Using HTML & CSS, take the following mock and implement a list of company models. You are not expected to be tying data in javascript objects to HTML elements in this question. You are only expected to create an HTML file that implements the mocks. You can create an external CSS file if you wish, the CSS doesn't need to be included in the HTML file. You are not allowed to use any external libraries like Bootstrap in this question.

Mock:

https://s3.amazonaws.com/ryse-static/programming-test/mock.png

Company Logo Examples:

https://s3.amazonaws.com/ryse-static/programming-test/facebook.png, https://s3.amazonaws.com/ryse-static/programming-test/google.png, https://s3.amazonaws.com/ryse-static/programming-test/reelio.png, https://s3.amazonaws.com/ryse-static/programming-test/twitter.jpg

Typography:

font-family: Open Sans, sans-serif;
color: #727272;

Background:

background-color: #f1f1f1;
  • SEE ATTACHED FILES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment