V

@Viveck

Last Stone Weight

NodeJS
1 year ago
// MaxHeap implementation using a binary heap class MaxHeap { constructor() { this.heap = []; } insert(value) { this.heap.push(value); this._bubbleUp(); }

Max subarray Sum

NodeJS
1 year ago
// https://chatgpt.com/share/670a5338-363c-8002-bbdc-58d14c79c27c arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] function maxSum(arr) { let maxCurrent = arr[0], maxGlobal = arr[0]; for (let i = 1; i < arr.length; i++) { maxCurrent = Math.max(arr[i], (maxCurrent + arr[i]))

Learn Async Await and Promises

NodeJS
1 year ago
const fetchData = new Promise((resolve, reject) => { const success = true; if (success) { resolve("Data received!"); } else { reject("Error fetching data"); } }); fetchData

ActiveRecord queries

Ruby
1 year ago
puts "Having" User.joins(:payment_requests).select("DISTINCT ON (users.id) users.*").where('payment_requests.created_at >= ?', '2024-01-01').group('users.id').having("SUM(payment_requests.amount) > ?", 0.3728e2) puts "Includes (Prevents N+1 query problem)" # This code block makes a single SQL query that makes a left outer joins and combines both associated records # and doesn't contain all record but only the filtered records based on provided inputs User.includes(:payment_requests).wh

Maximum Index difference in an array

NodeJS
1 year ago
// class Solution { // maxIndexDiff(arr, n) { // if (n === 1) { // return 0; // } // let maxDiff = 0; // let i = 0, j = 0; // let LMin = new Array(n); // let RMax = new Array(n);

Persistent systems - Interview

Ruby
1 year ago
p "====================Interview questions====================" p "1. what is singleton method?" p "2. class_eval vs instance_eval" p "3. cluster vs non-cluster index" p "4. locking strategy(optimistic/pesimistic)"

Integers

Ruby
1 year ago
p "===================Reverse an integer===================" # https://leetcode.com/problems/reverse-integer/ def reverse_integer(x) n = x negative = n < 0 n = n.abs reversed = 0

Arrays

Ruby
1 year ago
p "==========Insert at index==========" def insert_at_index(arr, size_of_array, index, element) arr.insert(index, element) end p insert_at_index([1,2,3,4,5,6], 6, 3, 80) p "==========Array leaders=========="

Arrays. - ZTM - JS

NodeJS
1 year ago
// Implementation of an Array class class Arr { constructor() { this.length = 0; this.data = {}; } get(index) { return this.data[index];

Arrays - ZTM

Ruby
1 year ago
# Arrays Introduction strings = ["a", "b", "c", "d"] # 4x4 = 16 bytes of storage # push strings.push("e") p strings

Matrix and Hashing - JS

NodeJS
1 year ago
console.log("===========Destructive approach===============") let mat = [[10,2,5,20], [30,15,25,6], [1,3,11,6]] function swapMat(mat) { for (let i = 0; i < mat.length-1; i++) { [mat[i][0], mat[i][mat[i].length - 1]] = [mat[i][mat[i].length - 1], mat[i][0]]; } return mat }

Matrix and Hashing

Ruby
1 year ago
p "========================Traverse first and last element of a matrix's sub arrays===========================" matrix = [[10,2,5,20], [30,15,25,6], [1,3,11,6]] p matrix.each{|arr| arr[0], arr[-1] = arr[-1], arr[0]}

Sorting techniques

NodeJS
1 year ago
// let arr = [64, 25, 12, 22, 11] // function selection_sort(arr) { // for (let i = 0; i < arr.length-1; i++) { // min_idx = i // for (let j = i+1; j < arr.length; j++) { // if (arr[i] < arr[min_idx]) { // min_idx = j // } // let temp = arr[min_idx];

Power Of Numbers

NodeJS
1 year ago
// Problem - https://www.geeksforgeeks.org/problems/power-of-numbers-1587115620/1 class Solution { constructor() { this.mod = 1000000007n; // Converting it to BigInt } power(N, R) { return Number(this.getPowerValue(BigInt(N), BigInt(R))); // Once we get the returned value as a BigInt number we convert it back to number. } getPowerValue(Num, Pow) {

Binary search

Ruby
1 year ago
p "==========================Binary search========================" def binary_search(arr, target) low = 0 high = arr.length - 1 while low <= high mid = low + (high - low) / 2 if arr[mid] == target return mid

All Recursion problems

Ruby
1 year ago
def recursiveSum(n) return 0 if n == 0 n + recursiveSum(n-1) end puts "==============Sum of N natural numbers===============" num = recursiveSum(10) puts num puts "==============Ascending ordered numbers==============="

Reverse a string

Ruby
1 year ago
def reverse_string(str) (str.size/2).times{|x| str[x], str[-x-1] = str[-x-1], str[x]} puts str end reverse_string("adhriyaa")

Strings

Ruby
1 year ago
p "================Palindrome string ?================" def is_palindrome(str) str = str.gsub(/[^A-Za-z0-9]/, '').downcase puts (str.size/2).times.all?{|x| str[x] == str[-x-1]} end is_palindrome("A Man, A Plan, A Canal, Panama!")

Sum of n natural numbers

Ruby
1 year ago
def sum_of_n_numbers(n) puts n*(n+1)/2 end sum_of_n_numbers(3)

Reverse an Integer

Ruby
1 year ago
def reverse(x) n = x negative = n < 0 n = n.abs reversed = 0 while n != 0 remainder = n % 10 reversed = reversed * 10 + remainder n /= 10