Current Status | Stats |
---|---|
Total Problems | 188 |
Note: Some of the code here is old and was written when I was learning C++. It might be possible that code is not safe or making wrong assumptions. Please use with caution. Pull requests are always welcome.
Problem | Solution |
---|---|
Find the nth node of linked list from last. | nthToLastNode.cpp |
Add numbers where each digit of the number is represented by node of a linkedlist. Give output as a linked list. | add_two_numbers_lists.cpp |
Swap nodes of a linkedlist without swapping data. | swapNodesWithoutSwappingData.cpp |
Reverse a linked list, iteratively and recursively | reverseLinked 8000 ListIterAndRecurse.cpp |
Given a linked list, reverse alternate nodes and append at the end. | reverseAlternateNodes.cpp |
Only given a node pointer, delete the node from the linked list. | deleteNode.cpp |
Delete the entire linkedlist. | deleteLinkedlist.cpp |
Print middle node of linkedlist without iterating twice. | printMiddleNode.cpp |
Determine if a linked list is a pallindrome. | listPallindrome.cpp |
Insert data in a sorted linked list. | insertInASortedLinkedList.cpp |
Determine the intersection(merging) point of two given linked list. | findIntersectionPointOfLists.cpp |
Clone a linkedlist which has next and an random pointer, Space Complexity - O(1). | cloneListWithRandomPtr.cpp |
Given a sorted linked list with duplicates, remove duplicates in one iteration. | removeDuplicatesFromSortedList.cpp |
Using Floyd's cycle finding algorithm, detect if a linkedlist contain cycle, if it does contain cycle, remove the loop | floyedCycleDetection.cpp |
Sort a linked list using merge sort | merge_sort.cpp |
Given a singly linked list L0 -> L1 -> … -> Ln-1 -> Ln. Rearrange the nodes in the list (in place) so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 .... | rearrange_list.cpp |
Include contains single header implementation of data structures and some algorithms.
Data Structure/Algorithm | Implementation |
---|---|
Generic Macros and Algorithms like swap, random number generation | generic.h |
Generic Stack Implementation | stack.h |
Generic Queue Implementation | queue.h |
Generic List Implementation | list.h |
Binary Search Tree Implementation | binarySearchTree.h |
Quick Sort Implementation | quickSort.h |
Merge Sort Implementation | mergeSort.h |
Selection Sort Implementation | selectionSort.h |
Bubble Sort Implementation | bubbleSort.h |
Linux Kernel Double LinkedList Implementation | double_linked_list.h |
Generic Graph Implementation (Adjacency List) | graph.h |
Heap Sort Implementation | heap_sort.h |
My own string library implementation | pstring.h pstring.cpp |
Problem | Solution |
---|---|
Determine if a number is a power of 2. | power_of_2.cpp |
Add two binary number represented as string. | addBin.cpp |
Determine the next power of 2 for a given number. | next_power_of_2.cpp |
Using bit manipulation determine if a number is multiple of 3. | multiple_of_3.cpp |
Determine endianess of the machine, print a number in reverse Endianess. | reverseEndianness.cpp |
Find the parity of given number. | find_parity.cpp |
Implement fast multiplication of a number to 7 using bit manipulation. | multiply_by_7.cpp |
Reverse bits of unsigned integer (two methods - Reversing bit by bit & divide and conquer). | reverseBitsOfAnInteger.cpp |
Small function to determine position of right most set bit in a given integer. | right_most_set_bit.cpp |
Given a vector of numbers, only one number occurs odd number of times, find the number. | find_odd_one_out.cpp |
Given two integers, determine if their sum would be interger overflow. | integerOverflow.cpp |
How many bit flip operation would require to convert number A to B. | countNumberOfBitFlips.cpp |
Given a number x and two positions (from right side) in binary representation of x, write a function that swaps n right bits at given two positions and returns the result. It is also given that the two sets of bits do not overlap. | swapSetOfBits.cpp |
Add two numbers without using any arithmetic operators | addition_without_operators.cpp |
Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations:
|
counter_game.cpp |
Determine if two integers are of opposite signs. | check_opposite_signs.cpp |
Swap two bits at position p and q of a given integer. | swapBits.cpp |
Check if a number is power of 4. | check_if_power_of_4.cpp |
Problem | Solution |
---|---|
Problem 1-1 : Edition 6: Write an algorithm to determine whether a string has unique characters or not. Can we do it without using additional data structures? | 1-1-hasUniqueChars.cpp |
Problem 1-2 : Edition 5: Reverse a string when you are a pass a null terminated C string. | 1-2-edi5-reverseString.cpp |
Problem 1-2 : Edition 6: Given two strings, determine if one is permutation of other. | 1-2-perm-strings.cpp |
Problem 1-3 : Edition 5: Write an algorithm to remove duplicate chars from a string. | 1-3-edi5-removeDuplicates.cpp |
Problem 1-3 : Edition 6: URLify: Replace all the spaces in a string with '%20'. Preferebly Inplace | 1-3-URLify.cpp |
Problem 1-4 : Edition 6: Given a string, write a function to check if it is a permutation of a pallindrome. | 1-4-pallindrome-permutations.cpp |
Problem 1-5 : Edition 6: There are three possible edits that can be performed on a string - Insert a char, Delete a char, Replace a char. Given two strings, determine if they are one or 0 edit away. | 1-5-one-edit-away.cpp |
Problem 1-6: Implement a method to perform basic string compression. Example string aabcccccaaa should be compressed to a2b1c5a3, however if compressed string is bigger than original string, return original string | 1-6-string-compression.cpp |
Problem 1-7: Rotate the matrix clockwise( & anticlockwise) by 90 degrees | 1-7-matrix-rotation.cpp |
Problem 1-8: Write an algorithm such that if an element of MxN matrix is 0, its entire row and column is set to 0. | 1-8-zero-matrix.cpp |
Problem 1-9: Given two strings s1 and s2, determine s2 is rotation of s1 using only one call to a function which checks whether one string is rotation of another. | 1-9-string-rotation.cpp |
Problem 2-1: Remove duplicates from an unsorted linked list. What if no temporary buffer is allowed. | 2-1-remove-dups.cpp |
Problem 2-2: Determine kth node from the last of a singly linked list. (Iterative and Recursive Approaches) | 2-2-kthToLast.cpp |
Problem 2-3: Implement an algorithm to delete a node in the middle of a singly linked list | 2-3-delete-middle-node.cpp |
Problem 2-4: Partition a linked list around a value x, all the nodes smaller than x come before all the nodes greater than equal to x | 2-4-partition.cpp |
Problem 2-5: You have two numberse represented by a linked list where each node contains a single digit. The digits are stored in reversed order, such that 1's digits are at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.Example:
|
2-5-add-lists.cpp |
Problem 2-6: Determine if linked list is palindrome( 2 iterative and one recursive approach | 2-6-palindrome.cpp |
Problem 2-7: Determine if two singly linked list intersect, if yes, return the intersecting node. The intersection is defined based on reference not on values | 2-7-intersection.cpp |
Problem 2-8: Detect if the linked list have a loop, Find the start node of the loop and remove the loop | 2-8-loop-detection.cpp |
Problem | Solution |
---|---|
Nth Fibonacci term using different memoization techniques | fibonacci.cpp |
Longest Common Subsequence Problem | lcs.cpp |
Maximum Value Contigous Subsequence Problem wiki | max_subsequence.cpp |
Catalan number - Count the number of possible Binary Search Trees with n keys | catalan_number.cpp |
Calculate the number of unique paths from source origin (0, 0) to destination (m-1, n-1) in a m x n grid. You can only move either in down or right direction. | unique_paths.cpp |
0-1 Knapsack Problem: Imagine you are a thief and you want to steal things with room full of things. You have a knapsack which can handle maximum capacity of weight W, and you want to fill it up such that it's worth is maximum. Being an intelligent thief, you know weights and values of each item in room. How would you fill your knapsack, such that you get the maximum possible value, such that you can only fill upto capacity W. | 0_1_knapsack_problem.cpp |
Problem | Solution |
---|---|
Iterative Level order traversal of Tree using queue | levelOrderTraversalIterative.cpp |
Recursive Level order traveral of Tree | levelOrderTraversalRecursive.cpp |
ZigZag Traversal of Tree | zigZagTraversal.cpp |
Predecessor and Successor of a given node in Binary Search Tree | predecessorSuccessor.cpp |
Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree. | lowest-common-ancestor.cpp |
Given a binary tree (unlike binary search tree), find the Lowest Common Ancestor (LCA). | lowest-common-ancestor-binary-tree.cpp |
Given a binary tree, print out all of its root-to-leaf paths one per line. | printAllRootToLeafPath.cpp |
Determine if a tree is sum tree. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree. | sumTree.cpp |
Convert a tree to sumTree, such that each node is sum of left and right subtree of the original tree. | convert_to_sum_tree.cpp |
Convert a sorted array to balanced binary search tree. | sortedArrayToBST.cpp |
Given a binary tree, generate sum of each vertical column. | verticalSum.cpp |
Given a binary tree and key, node with key exists in tree. Find all the ancestors of the node with key, ancestor here are the nodes which are in straight path from node to root. | node_ancestors_in_root_path.cpp |
Given a binary tree and key, return the level of the node with key. Root is at level 1, and if node with key does not exists in tree, return 0 | level_of_node.cpp |
Given a binary tree, find all the paths from root to nodes, whose sum is k. | k_sum_paths.cpp |
Given a binary tree, print its nodes level by level in reverse order. i.e. all nodes present at last level should be printed first followed by nodes of second-last level and so on.. All nodes for any level should be printed from left to right. | reverseLevelOrderTraversal.cpp |
Invert a binary tree, recursively and iteratively. | invert_a_tree.cpp |
Given a Binary Search Tree, find ceil and floor of a given key in it. If the given key lie in the BST, then both floor and ceil is equal to that key, else ceil is equal to next greater key (if any) in the BST and floor is equal to previous greater key (if any) in the BST | floor_ceil_bst.cpp |
Find kth smallest element in a binary search tree | kth_smallest.cpp |
Validate if a given binary tree is a binary search tree. | validate_bst.cpp |
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. | find_target_k.cpp |
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Also, to note that the target value is a floating point. There will be only one unique value which is closest to the target. | closest_bst_value.cpp |
Given a binary tree, traversing preorder, construct a string output containing node values and parenthesis. The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree. Examples in code file | string_from_tree.cpp |