This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Author: Shubham Mishra | |
class TreeNode | |
attr_accessor :value, :left, :right | |
def initialize(value) | |
@value = value | |
@left = nil | |
@right = nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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..] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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';") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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';") |