Skip to content

Instantly share code, notes, and snippets.

@rishabg
rishabg / dimepenny.rb
Last active September 18, 2020 16:44
# needs ruby > 2.6
# if we are allowed to move only 'pairs' of coins (not necessarily one coin of each type) from the ends
$possibilities = 0
def find_move(initial_input, input, output, iteration)
return nil if initial_input == input && iteration > 0
return nil if iteration > 5
$possibilities += 1
return [input] if input == output
left = find_move(initial_input, input.rotate(2), output, iteration + 1)
@rishabg
rishabg / sunspot_tasks.rake
Created July 24, 2020 04:10 — forked from lassebunk/sunspot_tasks.rake
Reindex Sunspot Solr gracefully
# This task can be used reindex Solr without dropping your index first.
# This is ideal for production environments where a live, working index is critical.
#
# Put this in lib/tasks/sunspot_tasks.rake
namespace :sunspot do
task reindex_gracefully: :environment do
Dir.glob(Rails.root.join('app/models/**/*.rb')).each { |path| require path }
sunspot_models = Sunspot.searchable
#Question to candidate
# In this 'real world' code, please do the following:
# 1. Identify code smells and other issues
# 2. Based on discussion suggest a refactoring and discuss options.
class Event < ActiveRecord::Base
def add_charge(asset,opts={})
context = {'Person' => [self.owner], 'Group' => [self.group], 'Institution' => (self.group.institutions rescue [])}
if opts[:price_id]
@rishabg
rishabg / array_sum.rb
Last active January 17, 2019 20:23
Array sum
# Given an array of integers and given a number K,
# find a pairs of elements (x,y) in the array
# whose summation is equal to K i.e x + y == K
# for example find_pair([1,2,3,4,5], 6) returns [2,4] OR [5,1]. Both outputs are acceptable.

Given an array of distinct elements, rearrange the elements of array in 'zig-zag' fashion. The converted array should be in form a < b > c < d > e < f.

Example:

Input:  arr = [4, 3, 7, 8, 6, 2, 1]
Output: [3, 7, 4, 8, 2, 6, 1]

Input:  arr =  [1, 4, 3, 2]
Output: [1, 4, 2, 3]
# Discuss how you would speed this up Given that:
# 1. CSV file can be very large
# 2. Processing happens as within a webrequest (server timeout = 2 min)
# 3. Not all optimizations are code-related. You are free to ask about possible functionality changes or side-effects.
# Hint: There are multiple issues here. First try to list all of them before attempting possible solutions
# Hint: Assume that the file is already present locally (ignore the network transfer process for the file)
# As a final step: Discuss how you would go about investigating and improving: slow_method_that_updates_multiple_models
//Ask if the candidate has front-end experience
//Let's say you have a "class" Vehicle
function Vehicle(...){
...
}
Vehicle.prototype.honk = function(){
console.log("honk");
}
# Discuss how you would speed this up Given that:
# 1. CSV file can be very large
# 2. Processing happens as within a webrequest (server timeout = 2 min)
# 3. Not all optimizations are code-related. You are free to ask about possible functionality changes or side-effects.
# Hint: There are multiple issues here. First try to list all of them before attempting possible solutions
# Hint: Assume that the file is already present locally (ignore the network transfer process for the file)
# As a final step: Discuss how you would go about investigating and improving: slow_process_that_updates_multiple_models
require "em-synchrony"
require "em-synchrony/em-http"
require 'net/http'
require 'benchmark'
require 'httparty'
require "em-synchrony/fiber_iterator"
url_list = ['https://msdn.microsoft.com/en-us/library/windows/desktop/aa366720(v=vs.85).aspx',
'http://www.oracle.com/technetwork/java/javase/tech/largememory-jsp-137182.html',
'http://www.oracle.com/technetwork/java/embedded/javacard/downloads/default-1970005.html',
@rishabg
rishabg / jsquiz.js
Last active December 25, 2015 03:29
// 1. Can you predict what will be 'hollered'.
// 2. Walk through the execution as you think out loud.
function holler(val, name, scope){
console.log("[ "+ scope +" ] variable '" + name + "' has value: " + val)
}
var a = '2';
var c = 'c';