Learn C, C++, Data Structures, Algorithms, Design Patterns, Coding Patterns, System Design
FreeCpp.com is your ultimate destination for mastering C++ interview preparation, data structures, design patterns, coding patterns, and STL!!
Hello, what would you like to learn about?
Programming Language’s
DSA
System Design
Latest Posts:
- 1.5) Self-Balancing Binary Search Trees (BSTs)BSTs offer fast search, insert, and delete operations when they are “balanced” – meaning the tree’s height is relatively small and comparable to the logarithm of the number of nodes.
- 1.4) Find the Floor of given value in BSTThe “floor” of a given value is the largest value in the tree that is less than or equal to the given value. In other words, it’s the “closest” value in the tree that is not greater than the given value.
- 1.3) Binary Search Tree DeletionWhen deleting a node in a Binary Search Tree (BST), there are three possible cases to consider: a)Node has no children (leaf node) b) Node has one child c) Node has two children
- 1.2) Binary Search Tree ImplementationBST C++ code that creates a simple Binary Search Tree and inserts elements into it while maintaining the BST property: elements in the left subtree are smaller, and elements in the right subtree are larger.
- 1.1) Introduction to Binary Search TreeFor every node ‘N’, all elements in the left subtree of ‘N’ are less than the value of ‘N’, and all elements in the right subtree of ‘N’ are greater than the value of ‘N’.
- 3.8) Iterative Postorder Traversal of Binary TreeWrite a program to perform an iterative post order traversal and print the values of the nodes as they are visited.
- 3.7) Iterative Preorder Traversal of Binary TreeGiven a binary tree, write a program to perform an iterative preorder traversal and print the values of the nodes as they are visited.
- 3.6) Iterative Inorder Traversal of Binary TreeGiven a binary tree, write a program to perform an iterative in-order traversal and print the values of the nodes as they are visited.
- 3.5) Serialize/Deserialize Binary TreeTo convert a binary tree into a string representation (serialization) and then converting it back from the string to the original binary tree (deserialization).
- 3.4) Count Nodes in Complete Binary TreeGiven a complete Binary Tree, write a function to count and return the total number of nodes present in the tree.
- 2.5) Find whether Binary Tree satisfies the Children Sum Property or not?The Children Sum Property states that, for each node in a Binary Tree, the value of the node must be equal to the sum of the values of its left and right children. If the node has no children, then the property is still satisfied.
- 2.4) To Print Nodes at K DistanceGiven a Binary Tree and an integer value K, write a function to print all the nodes that are at a distance K from the root node.
- 2.3) Find Height of Binary TreeGiven a Binary Tree, write a function to find and return the height of the tree. The height of a Binary Tree is the maximum number of edges on the longest path from the root node to any leaf node.
- 2.2) Find Maximum value among all nodes in the treeWrite a function to find and return the maximum value among all nodes in the tree.
- 2.1) Size of Binary TreeThe size of a Binary Tree is the total number of nodes present in the tree. It represents the count of nodes, which includes both internal nodes (with children) and leaf nodes (without children). Calculating the size of a Binary Tree involves traversing the tree and counting the nodes.
- 1.7) Binary Tree Zig-Zag traversal (Spiral Form Traversal)Spiral form traversal of a Binary Tree involves traversing the nodes level by level, alternating the direction between left to right and right to left. This creates a “zigzag” pattern as you move down the levels.
- 1.6) Level Order traversal of a Binary TreeLevel Order traversal explores nodes level by level, visiting all nodes at a given level before moving to the next level. This traversal order is useful for tasks like breadth-first search, finding the shortest path, or printing the tree in a structured way.
- 1.5) Post-Order traversal of a Binary TreePost-Order traversal visits nodes of a Binary Tree in the order: Left-Right-Root. This means you start by visiting the left child, then the right child, and finally the current node (root).
- 1.4) Pre-Order Traversal of Binary TreePre-Order traversal visits nodes of a Binary Tree in the order: Root-Left-Right.This means you start by visiting the root, then move to the left child, and finally to the right child.
- 1.3) In-Order traversal of Binary TreeIn-Order traversal is a way of visiting all nodes of a Binary Tree following a specific order: Left-Root-Right. This means you start from the left child, then visit the root, and finally move to the right child.
- 1.2) Binary TreeA Binary Tree is a type of data structure where each node can have at most two children. It’s like a branching structure where each node has the potential to lead to two other nodes. This structure is used in various computer science applications to organize and manage data.
- 1.4) Is Subsequence or not?Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
- 1.3) Counting BitsGiven a non-negative integer n, for every i (0 ≤ i ≤ n), calculate the number of 1’s in their binary representation and return an array.
- 1.2) Fibonacci NumberThe Fibonacci numbers are a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Given an integer n, find the n-th Fibonacci number.
- Minimum Cost to Hire K WorkersThere are N workers. The i-th worker has a quality quality[i] and a minimum wage expectation wage[i]. Now you want to hire exactly K workers to form a paid group. When hiring a group of K workers, you must pay them according to the following rules:
- Rearranging Strings K Distance ApartGiven a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k apart. If no such arrangement is possible, return an empty string.
- Find K Pairs with Smallest SumYou are given two integer arrays nums1 and nums2 sorted in non-decreasing order and two integers k and target. Find k pairs (a, b) from the arrays such that a + b is the smallest possible value, and a + b is less than or equal to target. Return the pairs in sorted order.
- Kth Smallest Element in a Sorted MatrixGiven a square matrix matrix, where each row and column is sorted in non-decreasing order, find the kth smallest element in the matrix.
- Merge K Sorted ListsYou are given an array of k linked lists, each linked list is sorted in ascending order. Merge all the linked lists into one sorted linked list and return it.
- 1.6) Find Combination Sum to TargetGiven an array of distinct integers candidates and a target integer target, return all possible combinations of candidates where the chosen numbers sum to target. You may use the same number multiple times. The combinations should be returned in any order.
- 1.5) Palindrome Partitioning of StringGiven a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
- 1.4) Letter Combinations of a Phone NumberGiven a string containing digits from 2 to 9 inclusive, return all possible letter combinations that the number could represent on a telephone keypad. The mapping of digits to letters is the same as that commonly found in a telephone keypad (e.g., 2 is associated with ‘abc’, 3 is associated with ‘def’, etc.).
- 1.3) Return all possible PermutationsGiven a collection of distinct integers, nums, return all possible permutations of the integers.
- 1.2) SubsetsGiven a set of distinct integers nums, return all possible subsets (the power set) of the set.
- Find Right IntervalGiven a list of intervals intervals, where intervals[i] = [start_i, end_i], for each interval i, find the index of the interval j such that start_j is the smallest possible value that is equal to or greater than end_i. If there is no such interval j, the output should be -1.
- IPO ProblemYou are given k projects, each with a capital value and a profit value. You are also given an initial capital. The i-th project requires capital[i] capital to start, and the profit of the i-th project is profits[i]. Once a project is selected, you can invest its capital and start it to get the profit. After each project is finished, you can choose to reinvest the profit back into one of the available projects.
- Sliding Window MedianGiven an array of integers nums and an integer k, you need to find the median of each window of size k moving from left to right in the array. The window slides by one position each time.
- Find Median from Data StreamWrite program to Find Median from Data Stream
- Concatenated WordsGiven a list of words, you need to find all the concatenated words in the list. A concatenated word is defined as a word that can be formed by concatenating at least two other words from the given list.
- Design Add and Search Words Data StructureDesign a data structure that supports adding new words and searching for words with a given pattern.
- Implement Trie (Prefix Tree)A Trie (Prefix Tree) is a tree-like data structure used to efficiently store a dynamic set of strings while allowing efficient prefix search. In this problem, you need to implement a Trie data structure that supports the following operations:
- Word BreakGiven a string s and a dictionary of words dict, you need to determine if s can be segmented into a space-separated sequence of one or more dictionary words.
- Minimum Height TreesYou are given a tree represented as an undirected graph, where each node has a unique value between 0 and n-1. The tree is given as a list of edges. You need to find and return all the “root” nodes of the minimum height trees (MHTs).
- Alien DictionaryGiven a list of words sorted in a special alien language, find the order of characters in that language.The alien language is represented by a list of words where each word consists of lowercase letters. However, the order of the letters in the alien language is unknown.You need to determine the order of characters in the alien language based on the given list of words.
- Course ScheduleYou are given a total number of courses (n) and a list of prerequisites represented as pairs of course numbers. The prerequisites indicate that to take course prerequisites[i][0], you must first complete course prerequisites[i][1]. It is guaranteed that a valid ordering of courses exists.
- MinesweeperYou are given a 2D grid representing a Minesweeper game. You need to reveal the cells of the grid according to the following rules:
- Count Sub IslandsYou are given two 2D grids, grid1 and grid2, both representing islands, where grid1 represents the original island and grid2 represents a sub-island formed by cutting out a rectangle from grid1. The values 1 in the grids represent land, and 0 represents water.
- Find All Groups of FarmlandYou are given a 2D grid representing a piece of farmland, where the value 1 represents cultivated land and 0 represents uncultivated land. A group of farmland is a connected region of cultivated land, where a cell is considered connected to its adjacent cells horizontally and vertically. You need to find all groups of farmland and return their coordinates as a list of 4-element arrays.
- Walls and GatesYou are given a 2D grid representing a house, with rooms labeled as ‘INF’ for empty rooms, -1 for walls, and 0 for gates. You need to fill each empty room with the distance to the nearest gate. If it is impossible to reach a gate, leave the room labeled as ‘INF’.
- Surrounded RegionsGiven a 2D board containing ‘X’ (representing walls) and ‘O’ (representing open spaces), capture all regions surrounded by ‘X’. A region is considered surrounded if all its adjacent cells (top, bottom, left, and right) are walls or are out of bounds.