Difference between Increasing & Non-Decreasing Order

Let’s take an array for example: int arr[] = {1,2,3,4,5,6}; The above array is sorted in increasing and non-decreasing order. Now, let’s add a few duplicates. int arr[] = {1,1,2,2,3,4,4}; The above array is not in an increasing order. But it is in non-decreasing order.

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…

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;…

Palindrome LinkedList Recursive

Details Send the pointer to head(phead) and head itself. Recurse till next of head is not NULL. Start popping the frames. If *phead == *head, good to move to next frame-up. Before moving up, move phead to next // Recursive function to check if linked list is palindrome or not int checkPalindrome(struct Node** left, struct…

Notes on Memory Mapped Files using mmap

Introduction mmap maps a file to a process virtual address space. The file blocks are loaded as needed, in units of the system page size. Each I/O must align with the page size. A block load is handled as a page fault. mmap() can provide a private or shared mapping of a region. mmap() to…

SIngle Thread LRUCache in C++

#include <iostream> #include <list> #include <unordered_map> using namespace std; size_t capacity; class LRUCache { public: bool Lookup(int key, int *price) { auto it = hash_table.find(key); if (it == hash_table.end()) { return false; } *price = it->second.second; // update the key in the queue MoveToFront(key, it); return true; } void Insert(int key, int price) { auto…

One Way Communication with Pipe in Linux

Pipe uses kernel buffer to store data. It is a unidirectional read/write buffer and connects two processes. Each end (read or write) Pipe mimics a file operation. A process can choose to read or write (but not both). The processes have to use at least two pipes for two-way communication. Example Communication Parent process can…

%d bloggers like this: