Skip to content

Instantly share code, notes, and snippets.

@swithrow
Last active August 9, 2018 00:06
Show Gist options
  • Save swithrow/c5c46467e9530235c453d8032e3c42ec to your computer and use it in GitHub Desktop.
Save swithrow/c5c46467e9530235c453d8032e3c42ec to your computer and use it in GitHub Desktop.
Hearken Interview Puzzlers

Hearken Interview Puzzlers

Overview

Hi there! We've prepared the following code samples to get an idea of how you think about writing and maintaining code. Please take a look at the following code, and answer the corresponding questions. If you have suggestions about how the code could be improved, feel free to share those as well.

We're interested in how you communicate with team members of varying technical background, so stick to plain English whenever possible, but if you feel it's necessary to get technical, please do. Do your best, but don't spend more than one hour on this. All code samples here are written in Rails-idiomatic Ruby.

Good luck! Email your responses to tech at wearehearken dot com.

Questions

Sample 1

def is_blocked?(request)
  user_agent = request.user_agent
  fragments = [
    'SEOkicks-Robot',
    'SemrushBot',
  ]
  fragments.any? { |fragment| user_agent.include?(fragment) }
end
  1. What does the above method do and why would you need it?

Sample 2

def check_field(name:)
  encrypted_name = "encrypted_#{name}".to_sym
  decrypted = decrypt_string self.send(encrypted_name)
  clear_text = self.send(name)
  if decrypted.to_s == clear_text.to_s
    true
  else
    puts "#{self.id}##{name}:"
    puts "  crypt: [#{decrypted}]"
    puts "  clear: [#{clear_text}]"
    false
  end
end
  1. What does the above method do and under what circumstances would you need it?

Sample 3

def self.from_hash(hash)
  h = hash.with_indifferent_access
  CustomField.new(
    name:     h[:name].to_s,
    type:     h[:type]||DEFAULT_TYPE,
    value:    h[:value],
    required: h[:required].present?,
  )
end
  1. What is the purpose of calling .with_indifferent_access?
  2. What assumptions are made about the incoming values? What happens if these assumptions are incorrect?
  3. What is the purpose of calling .present? on the :required member of the hash?

Sample 4

def update_configuration(options)
  options.each_pair do |key, value|
    begin
      configuration.set key, value
    rescue Excon::Errors::SocketError => e
      self.errors[:base] << "error uploading an image for #{key}"
    end
  end
end
  1. Can you propose an explanation for the error handling code in the middle of the above method? How do you think it came to be?

Sample 5

def self.organizations_selector_to_ids(selector)
  included, excluded = [:include, :exclude].map(&selector.symbolize_keys).map { |a| a.to_a.map(&:to_i) }
  enabled_org_ids = Organization.enabled.pluck(:id)
  if included.present?
    enabled_org_ids & included
  else
    enabled_org_ids - excluded
  end
end
  1. Describe the behavior of the above method, given that Organization is an ActiveRecord model.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment