Skip to content

Instantly share code, notes, and snippets.

View codertcet111's full-sized avatar
💭
Consistency brings happiness

codertcet111

💭
Consistency brings happiness
View GitHub Profile
@codertcet111
codertcet111 / binary_tree_common_operations_with_class.rb
Created August 7, 2023 14:21
Binary tree common operations like height, width with ruby class
#Author: Shubham Mishra
class TreeNode
attr_accessor :value, :left, :right
def initialize(value)
@value = value
@left = nil
@right = nil
end
@codertcet111
codertcet111 / merge_sort_recursively_ruby.rb
Created August 7, 2023 13:52
merge sort recursively in ruby, Author: Shubham Mishra
#Author: Shubham Mishra
def merge_sort(arr)
# Base case: If the array has only one element or is empty, it is already sorted
return arr if arr.length <= 1
# Divide the array into two halves
mid = arr.length / 2
left_half = arr[0...mid]
right_half = arr[mid..]
@codertcet111
codertcet111 / quick_sort_recursively_ruby.rb
Last active August 6, 2023 15:14
Quick sort recursively in ruby
#Author: Shubham Mishra
def quick_sort(arr)
return arr if arr.length <= 1
# Choose the pivot element (for simplicity, we'll use the last element)
pivot = arr.pop
left = []
right = []
#Authore: Shubham Mishra
def heap_sort(arr)
n = arr.length
# Build a binary heap from the array
(n / 2 - 1).downto(0) do |i|
heapify(arr, n, i)
end
@codertcet111
codertcet111 / composite_index_query_optimisation.rb
Created May 29, 2023 16:56
Improve the Ruby on Rails query time by updating composite indexing
#Author: Shubham Mishra
#Table
Approval: assignee_id, deleted_at, is_visible, status, ...
#Query:
result = ActiveRecord::Base.connection.execute('
EXPLAIN ANALYZE
SELECT "approvals"."request_type"
FROM "approvals"
WHERE "approvals"."deleted_at" IS NULL
@codertcet111
codertcet111 / Rails_postgresql_check_unused_indexes_db.rb
Last active May 22, 2023 10:50
This script is to check unused DB indexes in Postgresql DB through rails console
results = ActiveRecord::Base.connection.execute("
SELECT
idstat.relname AS TABLE_NAME,
indexrelname AS index_name,
idstat.idx_scan AS index_scans_count,
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size,
tabstat.idx_scan AS table_reads_index_count,
tabstat.seq_scan AS table_reads_seq_count,
tabstat.seq_scan + tabstat.idx_scan AS table_reads_count,
n_tup_upd + n_tup_ins + n_tup_del AS table_writes_count,
@codertcet111
codertcet111 / longest_substring_without_repeating_character.rb
Last active June 10, 2023 09:02
Longest Substring Without Repeating Characters ruby solution
#Author: Shubham Mishra
#I/O: puts length_of_longest_substring("abcabcbb")
#O/P: 3
#Solution 1: Time optimised solution
def length_of_longest_substring(s)
i, j = 0, 0
sub_s = s[0]
max = 0
@codertcet111
codertcet111 / Longest_Palindromic_Substring.rb
Created May 21, 2023 07:34
Longest Palindromic Substring problem ruby solution
#Author: Shubham Mishra
#I/O: puts longest_palindrome("abbdbebffbebshsj")
#O/P: bebffbeb
def longest_palindrome(s)
return s if s.length == 1
st, en = 0, s.length - 1
max = 0
sub_s = ""
sub_s_arr = []
# config/initializers/active_record_query_timeout.rb
# Set statement timeout for each query
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") # 10 seconds
ActiveRecord::MigrationContext.prepend(Module.new do
def initialize(*args)
super
ActiveRecord::Base.connection.execute("SET statement_timeout = '0';")
if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") # 10 seconds
# Register an before migration callback to reset the timeout to 0 which means no timeout
ActiveRecord::Migration.before_migrate do
ActiveRecord::Base.connection.execute("SET statement_timeout = '0';")
end
ActiveRecord::Migration.after_migrate do
ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';")