Skip to content

Instantly share code, notes, and snippets.

View kuntalchandra's full-sized avatar
🎯
Focusing

Kuntal Chandra kuntalchandra

🎯
Focusing
View GitHub Profile
@kuntalchandra
kuntalchandra / ImageResizerMultiProcess.php
Created September 14, 2017 13:34
Forking multiple processes in PHP using pcntl_fork()
<?php
include("Lib.php");
class ImageResizerMultiProcess
{
private $sizes = [];
public function setSizes($sizes)
{
@kuntalchandra
kuntalchandra / cmd.sh
Last active August 1, 2022 05:53
Load testing example using Apache Bench (ab) to POST JSON to an API
# Shoot off 300 requests at the server, with a concurrency level of 10, to test the number of requests it can handle per second
# -p POST
# -H Authentication headers
# -T Content-Type
# -c Concurrent clients
# -n Number of requests to run
# -l Accept variable document length
# -k Connection keep-alive
# -v Verbosity level
@kuntalchandra
kuntalchandra / print_random.py
Created August 22, 2019 13:06
Python: MagicMock, Patch and Side effect
import time
import uuid
class PrintRandom(object):
def execute(self) -> None:
while True:
self.print_number(uuid.uuid1().int)
time.sleep(1)
@kuntalchandra
kuntalchandra / inorder_successor_bst_ii.py
Created September 15, 2020 10:56
Inorder Successor in BST II
"""
Given a node in a binary search tree, find the in-order successor of that node in the BST. If that node has no
in-order successor, return null.
The successor of a node is the node with the smallest key greater than node.val.
You will have direct access to the node but not to the root of the tree. Each node will have a reference to its
parent node. Below is the definition for Node:
class Node {
@kuntalchandra
kuntalchandra / pairs_with_specific_difference.py
Created April 29, 2020 13:51
Pairs with Specific Difference
"""
Given an array arr of distinct integers and a nonnegative integer k, write a function findPairsWithGivenDifference that
returns an array of all pairs [x,y] in arr, such that x - y = k. If no such pairs exist, return an empty array.
Note: the order of the pairs in the output array should maintain the order of the y element in the original array.
input: arr = [0, -1, -2, 2, 1], k = 1
output: [[1, 0], [0, -1], [-1, -2], [2, 1]]
input: arr = [1, 7, 5, 3, 32, 17, 12], k = 17
output: []
@kuntalchandra
kuntalchandra / stack.c
Last active March 2, 2022 23:50
Stack implementation using Linked List
/**
* Implementation of stack using Linked list
* Average Time Complexity
* Insert/delete Θ(1)
* Search Θ(n)
* Space complexity: O(n)
*/
#include <cstdlib>
#include <stdio.h>
#include <malloc.h>
@kuntalchandra
kuntalchandra / binary_tree_left_side_view.py
Created August 30, 2020 04:11
Number of Visible Nodes from Left
"""
There is a binary tree with N nodes. You are viewing the tree from its left side and can see only the leftmost nodes
at each level. Return the number of visible nodes.
Note: You can see only the leftmost nodes, but that doesn't mean they have to be left nodes. The leftmost node at a
level could be a right node.
Signature
int visibleNodes(Node root) {
Input
@kuntalchandra
kuntalchandra / pair_sums.py
Created August 12, 2020 03:36
Pair Sums
"""
Given a list of n integers arr[0..(n-1)], determine the number of different pairs of elements within it which sum to k.
If an integer appears in the list multiple times, each copy is considered to be different; that is, two pairs are
considered different if one pair includes at least one array index which the other doesn't, even if they include the
same values.
"""
from unittest import TestCase
def numberOfWays(arr, k):
@kuntalchandra
kuntalchandra / flatten_binary_tree.py
Created August 4, 2020 13:49
Flatten Binary Tree to Linked List
"""
Given a binary tree, flatten it to a linked list in-place.
For example, given the following tree:
1
/ \
2 5
/ \ \
3 4 6
@kuntalchandra
kuntalchandra / sort_nearly_sorted_array.py
Created March 11, 2020 13:32
Sort a nearly sorted (or K sorted) array
"""
Average time complexity: O(nLogk)
"""
from heapq import heapify, heappop, heappush
from unittest import TestCase
def sort_nearly_sorted_array(array: list, k: int) -> list:
heap = array[:k + 1]
heapify(heap)