Skip to content

Instantly share code, notes, and snippets.

@subratrout
Forked from somu45/Interview_questions.md
Created September 22, 2017 23:30
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 subratrout/f76e40dbfad97035cf59b2765839fb75 to your computer and use it in GitHub Desktop.
Save subratrout/f76e40dbfad97035cf59b2765839fb75 to your computer and use it in GitHub Desktop.
Ruby, Rails and Sql Specific Questions

Rails

  1. Fix the following snippet. Given a collection of 1000 draft Articles, update all of them to be published

    Post.where(status: :draft).each { |p| p.update_attributes(status: :published}
    #Ans. Post.where(status: :draft).update_all(status: :published)
  2. Given the following model tables

    users

    id username email
    2 Leslie leslie.dunn@mail.com
    3 Amy amy.cook@mail.com
    5 Gavin gavin.olson@mail.com
    7 Lucas lucas.carr@mail.com
    11 Jennie jennie.cruz@mail.com

    posts

    id user_id title
    10 2 Trump, in Poland, Asks if West Has the 'Will to Survive'
    11 3 Merkel Has to Deal With Trump, the Question Is How
    12 2 New Jersey Transit Train Derails at Pennsylvania Station
    13 5 Faces of Intermarriage, 50 Years After the Loving Case
    14 11 In Milan, Beer, History and, of Course, Fashion
    15 3 Review: Spider-Man (Again) and All That Sticky Kid Stuff
    16 7 Editorial: Showdown in Hamburg

    comments

    id post_id content
    10 11 There are various options that can be discussed
    11 12 This is crazy
    12 11 You could almost see her analyze Trump, run through the various scenarios and approaches for dealing with him
    13 15 Wow!!! Another somewhat positive review of a super hero movie.
    14 16 This is a very dangerous man who is making the U.S. into a backwater.
    15 12 They had been aware of the problem but had underestimated just how urgently it needed to be addressed

    Use ActiveRecord to present the following serialized response. Optimize your code for performance.

    RESPONSE

    {
      "users": [
        {
          id: 2,
          "username": "Leslie",
          "email": "leslie.dunn@mail.com",
          "posts": [
            {
              "id": 10,
              "title": "Trump, in Poland, Asks if West Has the 'Will to Survive'",
              "comments": []
            },
            {
              "id": 12,
              "title": "New Jersey Transit Train Derails at Pennsylvania Station",
              "comments": [
                {
                  "id": 12,
                  "content": "This is crazy"
                },
                ...
              ]
            },
            ...
          ]
        },
        ...
      ]
    
    }
    #Ans. 
    
    Gem : active_model_serializers
    
    class UserSerializer < ActiveModel::Serializer
        attributes :id, :username, :email
        has_many :posts
    end
    
    class PostSerializer < ActiveModel::Serializer
        attributes :id, :title
        has_many :comments
    end
    
    class CommentSerializer < ActiveModel::Serializer
        attributes :id, :content
    end
    
    class UsersController < ApplicationController
        def index
          users = User.all.includes(posts: [:comments])
          render json: users, each_serializer: UserSerializer
        end
    end
    
    Api -> /users

Ruby

  1. What is self.included? How would you use it in mixins?
    #Ans.  It is a class method that will be called automatically when another class or module try to include as their extension. Included module methods will be accesed with instance variable.
    eg: 
    module B
     def say
        "hello"
     end
    end
    class A 
      include B
    end
    A.new.say
  1. Why is it bad to use rescue in the following manner? What do you understand about the ruby exception hierarchy?

    begin
      do_something()
    rescue Exception => e
      puts e
    end
    #Ans.  Exception is class of root error in error hirarchy.  It can raise any error which not proper with the method or running process. Hence its better to use specific class or use standard class
    
    Eg: begin; 1+1=2; rescue Exception => e; puts e;end
    
    If there is issue with database, the Exception will be called which is not related with the process 1+1=2

SQL

  1. A cooperation booth sells gloves in singles and pairs. Each transaction may be a purchase of a single side of glove, or a combination of left/right gloves. It is invalid, however, when a combination of right-right or left-left gloves to be sold.

    Given the following data, write a SQL query to list the duplicate gloves (for both left-left and right-right) sold.

    Transaction

    id sold_at
    1 '2017-05-07 15:29:36 +0800'
    2 '2017-05-09 17:01:59 +0800'
    3 '2017-06-10 09:32:01 +0800'
    4 '2017-06-12 09:34:47 +0800'

    Gloves

    id transaction_id glove_type
    10 1 "right"
    11 1 "left"
    12 2 "left"
    13 3 "left"
    14 3 "left"
    15 4 "right"
    16 5 "left"
    17 4 "left"
    18 5 "left"
    19 6 "right"

    Your query result should be:

    id transaction_id glove_type
    14 3 "left"
    18 5 "left"
    #Ans.  SELECT transaction_id, glove_type, count(*) FROM gloves group by transaction_id, glove_type HAVING ( COUNT(*) > 1 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment