development

Quicksort in C++

// cat quicksort.cc #include <bits/stdc++.h> #include <algorithm> // std::swap using namespace std; int partition(int arr[], int low, int high); void qsort(int arr[], int start, int end) { int p; if (start < end) { p = partition(arr, start, end); qsort(arr, start, p – 1); qsort(arr, p + 1, end); } } int partition(int arr[], int […]

Read more
development

Basic HashMap in C++

Useful Points I use chaining for key collisions. The key is also stored with the payload. A GET operation searches the key in bucket’s linked-list and returns the value. #include<bits/stdc++.h> using namespace std; class HashNode { private: string key; string value; HashNode *next; public: HashNode(string key, string value) { this->key = key; this->value = value; […]

Read more
development

Longest Palindrome Substring in a String

DP based solution class Solution(object): def __init__(self): self.lookup = {} def isPalindrome(self, s, dp, i, k): j = i + k – 1 if s[i] == s[j] and dp[i + 1][j – 1]: return True return False def longestPalindrome(self, s): “”” :type s: str :rtype: str “”” size = len(s) if size < 2: return […]

Read more
development

Union-Find Problem

The problem is to find if a given edge (a, b) is connected in a graph with vertices {a, b, c, …}. Naive Solution – O(n^2) Maintain a parent array of size (len of vertex array) Update an edge (a, b) with the index a & b in the vertex array ParentArray[a] = b ParentArray[b] […]

Read more
development

Longest Substring without Repeating Characters

The solution uses a sliding window and uses O(n) time and O(n) space. There are many cases to take care of. We can skip the process string less than size 2. Iterate the string, checking each char as key in the hash, adding if not present. If not present in hash, add each char as […]

Read more
development

Rapid Learn Swagger OpenAPI

What is Swagger? A standard way to document REST APIs The documentation is described in a YAML or JSON document. OpenAPI has syntax and tags to write an API description. Why Use Swagger? Very standard It not just describes APIs but also serves as an APIs contract among teams. Each API specifies the expected request, […]

Read more
development

Pascal Triangle

def generate(self, numRows): “”” :type numRows: int :rtype: List[List[int]] “”” answer = [] if numRows == 0: return [] row = [1] answer.append(row) for x in range(2, numRows+1): # Prepare the row row = [1]*x # The first and last element on each row is 1, so skip those # indexes. for y in range(1, […]

Read more