Count Number of Bits to be Flipped to Convert A to B

Suppose, I have two numbers A and B. I need to find out how many numbers of bits needed to be changed to convert A to B. Like: A = 1101101 B = 1011011 ^^ ^^ Here, we need to change 4 bits to convert A to B How can I do this? Solution: 1. Read More →

Union and Intersection of two Linked Lists

Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. Order of elments in output lists doesn’t matter. Following are simple algorithms to get union and intersection lists respectively. Intersection (list1, list2) Initialize result list as NULL. Traverse list1 and look for its Read More →

What are the underlying data structures used for Redis?

Here is the underlying implementation of every Redis data type. – Strings are implemented using a C dynamic string library so that we don’t pay (asymptotically speaking) for allocations in append operations. This way we have O(N) appends, for instance, instead of having quadratic behavior. – Lists are implemented with linked lists. – Sets and Read More →

Redis Tutorial

Redis, developed in 2009, is a flexible, open-source, key value data store. Following in the footsteps of other NoSQL databases, such as Cassandra, CouchDB, and MongoDB, Redis allows the user to store vast amounts of data without the limits of a relational database. Additionally, it has also been compared to memcache and can be used, Read More →

Edit Distance – DP Problem

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Replace a character Input: str1 = “cat”, str2 = “cut” Output: Read More →

Graph – Breath First Search

breath first search

Below is a simple implementation of a graph and breath first search. The key is using a queue to store nodes. 1) Define a GraphNode 2) Define a Queue 3) Breath First Search uses a Queue Output: value: 2 value: 3 value: 5 Find value: 5 value: 4

Graph Data Structure

Graphs A tree only allows a node to have children, and there cannot be any loops in the tree, with a more general graph we can represent many different situations. A very common example used is flight paths between cities. If there is a flight between city A and city B there is an edge Read More →

Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is Read More →

Decode Ways Leetcode Java

Problem: A message containing letters from A-Z is being encoded to numbers using the following mapping: ‘A’ – 1 ‘B’ – 2 ‘Z’ – 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded message “12″, it could be decoded as “AB” (1 2) or Read More →

Min Stack Problem

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) — Push element x onto stack. pop() — Removes the element on top of the stack. top() — Get the top element. getMin() — Retrieve the minimum element in the stack. Solution: the StackMin class which keeps the Read More →

Finding All the Subsets of a Set – Backtracking Problem

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] We use the backtracking method to solve Read More →

Given a triangle, find the minimum path sum from top to bottom

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = Read More →