### HTTP POST Request Example Source: https://deckly.dev/designdeck An example of a standard HTTP/1.0 POST request structure, including the method, URL, headers, and body content. ```http POST https://example.com HTTP/1.0 Host: example.com User-Agent: Mozilla/4.0 Content-Length: 5 Hello ``` -------------------------------- ### Tabulation Example in Pseudocode Source: https://deckly.dev/?q= Illustrates the tabulation technique in dynamic programming using a bottom-up approach. It iteratively builds up solutions from the smallest subproblems to the larger ones, storing results in an array. This pseudocode shows a function to compute Fibonacci numbers. ```pseudocode tabFib(n) { mem[0] = 0 mem[1] = 1 for i = 2...n mem[i] = mem[i-2] + mem[i-1] return mem[n] } ``` -------------------------------- ### Memoization Example in Pseudocode Source: https://deckly.dev/?q= Demonstrates the memoization technique in dynamic programming using a top-down approach. It caches results of subproblems to avoid redundant computations. This pseudocode illustrates a recursive function that stores computed values in a 'mem' array. ```pseudocode f(x) { if (mem[x] is undefined) mem[x] = f(x-1) + f(x-2) return mem[x] } ``` -------------------------------- ### Reverse an array Source: https://deckly.dev/ Reverses an array in-place using two pointers. It swaps elements from the start and end, moving towards the center until the entire array is reversed. ```Java int i = 0; int j = a.length - 1; while (i < j) { swap(a, i++, j--); } ``` -------------------------------- ### Implement Dynamic Programming: Memoization vs Tabulation Source: https://deckly.dev/ Demonstrates the two primary approaches to dynamic programming. Memoization uses a top-down recursive approach with caching, while tabulation uses a bottom-up iterative approach to build solutions. ```pseudocode f(x) { if (mem[x] is undefined) mem[x] = f(x-1) + f(x-2) return mem[x] } ``` ```pseudocode tabFib(n) { mem[0] = 0 mem[1] = 1 for i = 2...n mem[i] = mem[i-2] + mem[i-1] return mem[n] } ``` -------------------------------- ### Get i-th Bit (Java) Source: https://deckly.dev/?q= Retrieves the value of the i-th bit in an integer. It creates a mask by left-shifting 1 by i positions. The operation is performed using a bitwise AND. If the result is non-zero, the i-th bit is 1; otherwise, it is 0. ```java boolean getBit(final int num, final int i) { return ((num & (1 << i)) != 0); } ``` -------------------------------- ### Implement Quicksort Algorithm in Java Source: https://deckly.dev/ An in-place sorting algorithm that uses a pivot element to partition the array into smaller and larger subsets. It offers O(n log n) average time complexity but is not stable. ```java void quickSort(int[] a) { quickSort(a, 0, a.length - 1); } void quickSort(int a[], int lo, int hi) { if (lo < hi) { int pivot = partition(a, lo, hi); quickSort(a, lo, pivot - 1); quickSort(a, pivot + 1, hi); } } int partition(int a[], int lo, int hi) { int pivot = a[hi]; int pivotIndex = lo; for (int i = lo; i < hi; i++) { if (a[i] <= pivot) { swap(a, pivotIndex++, i); } } swap(a, pivotIndex, hi); return pivotIndex; } ``` -------------------------------- ### 0/1 Knapsack Tabulation Implementation in Java Source: https://deckly.dev/ An iterative dynamic programming approach using a 2D table to build up the solution. It calculates the maximum profit for every capacity up to the target, ensuring optimal substructure usage. ```java public int solveKnapsack(int[] profits, int[] weights, int capacity) { int[][] a = new int[profits.length + 1][capacity + 1]; for (int row = 1; row < profits.length + 1; row++) { int value = profits[row - 1]; int weight = weights[row - 1]; for (int col = 1; col < capacity + 1; col++) { int remainingWeight = col - weight; if (remainingWeight < 0) { a[row][col] = a[row - 1][col]; } else { a[row][col] = Math.max( a[row - 1][col], value + a[row - 1][remainingWeight] ); } } } return a[profits.length][capacity]; } ``` -------------------------------- ### 0/1 Knapsack Memoization Implementation in Java Source: https://deckly.dev/ An optimized recursive approach using a 2D array to store previously computed results for specific capacity and item index combinations. This reduces redundant calculations, achieving O(n * c) time and space complexity. ```java public int knapsack(int[] profits, int[] weights, int capacity) { // Capacity from 1 to n Integer[][] a = new Integer[capacity][profits.length]; return knapsack(profits, weights, capacity, 0, 0, a); } public int knapsack(int[] profits, int[] weights, int capacity, int i, int sum, Integer[][] a) { if (i == profits.length || capacity == 0) { return sum; } // If value already exists, return if (a[capacity - 1][i] != null) { return a[capacity][i]; } // With int sum1 = knapsack(profits, weights, capacity, i + 1, sum, a); // Without int sum2 = 0; if (weights[i] <= capacity) { sum2 = knapsack(profits, weights, capacity - weights[i], i + 1, sum + profits[i], a); } a[capacity - 1][i] = Math.max(sum1, sum2); return a[capacity - 1][i]; } ``` -------------------------------- ### 0/1 Knapsack Brute Force Implementation in Java Source: https://deckly.dev/ A recursive approach to the 0/1 Knapsack problem that explores all possible combinations of items. It branches by either including or excluding the current item at each step, resulting in exponential time complexity. ```java public int knapsack(int[] profits, int[] weights, int c) { return knapsack(profits, weights, c, 0, 0); } public int knapsack(int[] profits, int[] weights, int c, int i, int sum) { if (i == profits.length || c <= 0) { return sum; } // Not int sum1 = knapsack(profits, weights, c, i + 1, sum); // With int sum2 = 0; if (weights[i] <= c) { sum2 = knapsack(profits, weights, c - weights[i], i + 1, sum + profits[i]); } return Math.max(sum1, sum2); } ``` -------------------------------- ### Find Median of a Number Stream using Two Heaps Source: https://deckly.dev/?q= Implements a median-finding algorithm using a max-heap for the lower half and a min-heap for the upper half of the data stream. It maintains balance between the two heaps to ensure efficient median calculation in O(log n) time. ```java PriorityQueue maxHeap = new PriorityQueue<>((a, b) -> b - a); PriorityQueue minHeap = new PriorityQueue<>(); public void insertNum(int n) { if (minHeap.isEmpty()) { minHeap.add(n); return; } Integer minSecondHalf = minHeap.peek(); if (n >= minSecondHalf) { minHeap.add(n); } else { maxHeap.add(n); } if (minHeap.size() > maxHeap.size() + 1) { maxHeap.add(minHeap.poll()); } else if (maxHeap.size() > minHeap.size() + 1) { minHeap.add(maxHeap.poll()); } } public double findMedian() { if (minHeap.size() == maxHeap.size()) { return (double) (minHeap.peek() + maxHeap.peek()) / 2; } if (minHeap.size() > maxHeap.size()) { return minHeap.peek(); } return maxHeap.peek(); } ``` -------------------------------- ### Populate Level-by-Level Traversal Array in Java Source: https://deckly.dev/ Uses a queue-based BFS algorithm to group tree nodes into lists representing each level. It processes a fixed number of elements per iteration based on the current queue size. ```Java public static List> traverse(TreeNode root) { List> result = new LinkedList<>(); Queue queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { List level = new ArrayList<>(); int levelSize = queue.size(); // Pop only levelSize elements for (int i = 0; i < levelSize; i++) { TreeNode current = queue.poll(); level.add(current.val); if (current.left != null) { queue.add(current.left); } if (current.right != null) { queue.add(current.right); } } result.add(level); } return result; } ``` -------------------------------- ### Dijkstra Algorithm Implementation Source: https://deckly.dev/ Implements Dijkstra's algorithm to find the shortest path from an initial vertex in a graph. It uses a priority queue to efficiently explore nodes and updates shortest distances and previous nodes. Dependencies include a GraphAjdacencyMatrix class and an Entry class for the priority queue. ```java void dijkstra(GraphAjdacencyMatrix graph, int initial) { Set visited = new HashSet<>(); int n = graph.vertex; int[] shortest = new int[n]; int[] previous = new int[n]; for (int i = 0; i < n; i++) { if (i != initial) { shortest[i] = Integer.MAX_VALUE; } } // Entry: key=vertex, value=distance so far PriorityQueue minHeap = new PriorityQueue<>((e1, e2) -> e1.value - e2.value); minHeap.add(new Entry(initial, 0)); while (!minHeap.isEmpty()) { Entry current = minHeap.poll(); int source = current.key; int distanceSoFar = current.value; // Get neighbours List edges = graph.getEdge(source); for (GraphAjdacencyMatrix.Edge edge : edges) { // For each neighbour, check the total distance int distance = distanceSoFar + edge.distance; if (distance < shortest[edge.destination]) { shortest[edge.destination] = distance; previous[edge.destination] = source; } // Add the element in the queue if not visited if (!visited.contains(edge.destination)) { minHeap.add(new Entry(edge.destination, distance)); } } visited.add(source); } print(shortest); print(previous); } ``` -------------------------------- ### Solve 0/1 Knapsack using Memoization Source: https://deckly.dev/?q= Optimizes the recursive knapsack approach by storing results of subproblems in a 2D array. This reduces redundant calculations, achieving a time and space complexity of O(n * c). ```java public int knapsack(int[] profits, int[] weights, int capacity) { Integer[][] a = new Integer[capacity][profits.length]; return knapsack(profits, weights, capacity, 0, 0, a); } public int knapsack(int[] profits, int[] weights, int capacity, int i, int sum, Integer[][] a) { if (i == profits.length || capacity == 0) { return sum; } if (a[capacity - 1][i] != null) { return a[capacity][i]; } int sum1 = knapsack(profits, weights, capacity, i + 1, sum, a); int sum2 = 0; if (weights[i] <= capacity) { sum2 = knapsack(profits, weights, capacity - weights[i], i + 1, sum + profits[i], a); } a[capacity - 1][i] = Math.max(sum1, sum2); return a[capacity - 1][i]; } ``` -------------------------------- ### Perform Breadth-First Search (BFS) Traversal in Java Source: https://deckly.dev/ Implements a level-order traversal of a graph using a Queue. It tracks visited nodes to prevent cycles and redundant processing. ```java Queue queue = new LinkedList<>(); Node first = graph.nodes.get(0); queue.add(first); first.markVisitied(); while (!queue.isEmpty()) { Node node = queue.poll(); System.out.println(node.name); for (Edge edge : node.connections) { if (!edge.end.visited) { queue.add(edge.end); edge.end.markVisited(); } } } ```