Skip to content

Instantly share code, notes, and snippets.

@adamki
Last active March 19, 2016 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamki/cde4d7c799d13afd285a to your computer and use it in GitHub Desktop.
Save adamki/cde4d7c799d13afd285a to your computer and use it in GitHub Desktop.
answers for KITEco

Q.1

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

MVC stands for Model View Controller. It is not a proprietary pattern or technokofy. It is simply a way to seperating tasks in a web application. The following parts can be broken up like so:

MODEL:  is an OO representation of Data in the System. The Model should be used to implement Business logic. In Rails, the model leverages Active Record to interact w/ Database and Plain Ruby Objects.

VIEW: Is used to prepare data for for presentation for the user. Should not contain logic. In Rails, use of html.erb or haml is fairly common. Can also use external templating if needed.

CONTROLLER: Coordinates and orhestrates all ofther peices of MVC system. Deals with the HTTP action(s) and calls the appropriate methods. A good exaample of Controller use would be to get the PARAMS hash and the vall the desired actions.

#Q.2 What does the following code do? company = Company.new({name: 'Pied Piper'}) company.save

This code make an instance of the Company class and assigns it the attribute "name". The attribute is a string Value, "Pied Piper". This is saved to a variable for later use. 

company.save performs a DB transaction and saves the record as a new row in the DB. This record could then be retrieved by using something and AR query like:

Company.find_by(name: "Pied Piper")

#Q.3 Where is the error in the following code?

How would you fix it?

({
  baz: 'foobarbaz',
  bar: function(){
    return 'foobar';
  },
  foo: function() {
    console.log(this.baz);
    setTimeout(function() {
      console.log(this.bar());
    }, 100);
  }
}.foo());

The issue is the incorrect use of this. The this instance will always refer to the point where it was called. Since Javascript is functionally scoped, the this within the setTimeout anon function is undefined since there is NO this.bar attribtute from within that fucntion scope.

You can use bind to get around this like so:

({
  baz: 'foobarbaz',
  bar: function(){
    return 'foobar';
  },
  foo: function() {
    console.log(this.baz);
    setTimeout(function() {
      console.log(this.bar());    /* The prblem is that "this" in the setTimeout scope is undefined. */
    }.bind(this), 100);           /* we can use .bind() OUTSIDE of the setTimeout function to declare "This" on this scope.   */
  }
}.foo());

#Q.4 Given an array of integer values, write a function in any language 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].

var array = [1, 2, 3, 3, 4, 5, 5, 6]
var results = [];
for (var i = 0, len = array.length - 1; i < len; i++) {
  if((results.indexOf(array[i]) == -1) && (array.indexOf(array[i], i + 1) != -1)) {
      results.push(array[i]);
   }
}
console.log(results);

Q.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?

If I wanted to collect a single specific record, I would likely just use: Company.find(10).funding_rounds to get an array for a given company.

If this isn't fast enough, of it I wanted to get multiple Companies records, I would eager load and avoid N+1 queries:

companies = Company.includes(:funding_rounds).limit(10)
 
companies.each do |company|
  puts company.funding_round.funding_in_usd
end

Q.6 :

Design a RESTful JSON CRUD API for the 2 models above. Include expected input, what the controller might look like as well as the HTTP response code & JSON response object. Be sure to include how to obtain a list of each model type. 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.

INDEX
// GET /funding_rounds
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Model as JSON, 200 success
//   Controller example:
//    respond_with FundingRounds.all
//   Input Example: 
//   Output Example: {funding_rounds: 
                      { id: 123, funding_in_usd: 123.25, company_id: 321 },
                      { id: 124, funding_in_usd: 456.56, company_id: 321 },
                      { id: 125, funding_in_usd: 789.89, company_id: 321 },
                      ...
                    }
SHOW
// GET /funding_rounds/:id
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Model as JSON, 200 success
//   Controller Example: 
//     respond_with FundingRounds.find(params[:id])
//   Input Example: {id: 123}
//   Output Example: {funding_round: { id: 123, funding_in_usd: 123.25, company_id: 321 }}
CREATE
// POST /funding_rounds/
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Index as JSON, 200 success
//   Controller Example: 
//     @funding_round = FundingRound.new(funding_round_params)
//     respond_with @funding_round if @funding_round.save
//   Input Example: {funding_in_usd: 111.11, company_id: 321}
//   Output Example: redirect_to :funding_rounds_path
UPDATE

NOTE: I dont believe that it would make sense to update an existing Funding Round. However, including it for the sake of completeness. Can be ommitted if needed.

// PUT /funding_rounds/:id
//   Controller Process:
//     - 204 on success
//     - Lookup Model, 404 on fail
//   Controller Example: 
//     respond_with FundingRounds.update(params[:id] , funding_rounds_params)
//   Input Example: {id: 123, funding_round_params}
//   Output Example: {funding_round: { id: 123, funding_in_usd: 123.25, company_id: 321 }}
DELETE

NOTE: I dont believe that it would make sense to update an existing Funding Round. However, including it for the sake of completeness. Can be ommitted if needed.

// DELETE /funding_rounds/:id
//   Controller Process:
//     - 204 on success
//     - Lookup Model, 404 on fail/nothing to Delete.
//   Input Example: {id: 123}
// NO JSON output

COMPANY API

INDEX
// GET /companies
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Model as JSON, 200 success
//   Controller example:
//    respond_with Companies.all
//   Input Example: 
//   Output Example: {companies: 
                       {id: 123, name: "Kite", description: "kite desc", logo_url: "https://logos.com/kite", score: 100},
                       {id: 321, name: "test co", description: "test co desc", logo_url: "https://logos.com/test_co", score: 434 },
 ...
                
}
SHOW
// GET /companies/:id
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Model as JSON, 200 success
//   Controller Example: 
//     respond_with Company.find(params[:id])
//   Input Example: {id: 123}
//   Output Example: {company: {id: 123, name: "Kite", description: "kite desc", logo_url: "https://logos.com/kite", score: 100}}
CREATE
// POST /companies/
//   Controller Process:
//     - Lookup Model, 404 on fail
//     - Render Model as JSON, 200 success
//   Controller Example: 
//     @company = Company(company_params)
//     respond_with @company if @company.save
//   Input Example:  {name: "Kite", description: "kite desc", logo_url: "https://logos.com/kite", score: 100}
//   Output Example: redirect_to companies_path
UPDATE
// PUT /companies/:id
//   Controller Process:
//     - 204 on success
//     - Lookup Model, 404 on fail
//   Controller Example: 
//     respond_with Company.update(params[:id] , company_params)
//   Input Example: {id: 123,  company_params}
//   Output Example: {company: {id: 123, name: "Kite", description: "kite desc", logo_url: "https://logos.com/kite", score: 100}}
DELETE

NOTE: I dont believe that it would make sense to update an existing Funding Round. However, including it for the sake of completeness. Can be ommitted if needed.

// DELETE /companies/:id
//   Controller Process:
//     - 204 on success
//     - Lookup Model, 404 on fail/nothing to Delete.
//   Input Example: {id: 123}
// NO JSON output

#Q.7 Using HTML & CSS, take the following mock and implement a list of company models. The JSON output you have on the front-end should be the JSON output for the /companies API route you created in question 2. 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.

http://codepen.io/adamki/pen/QNpyVZ

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