Tag Archives: Data Structures

Passing Car East or West Codility

A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. Array A contains only 0s and/or 1s: 0 represents a car traveling east, 1 represents a car traveling west. The goal is to count passing cars. We say that a pair of Read More →

Check if Any Anagram of a String is Palindrome or Not

Examples: Input: str = “aaaad” Output: 1 // “aaaad” is a anagram of a palindrome “aadaa” Input: str = “abcd” Output: 0 // “abcd” is not a anagram of any palindrome. A Simple Solution is to take the input string, try every possible rotation of it and return true if a anagram is a palindrome. Read More →

Print BST elements in range K1 and K2

Problem: Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Print all the keys of tree in range k1 to k2. i.e. print all x such that k1

Populate each next pointer to point to its next right node

Problem: Given a binary tree populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. You may assume that it is a perfect binary tree (i.e. all leaves are at the Read More →

Check if Two Trees are Identical

Problem: Given two binary trees, write a function to check if they are equal or not. Solution: Two binary trees are considered equal if they are structurally identical andthe nodes have the same value. Time Complexity: O(N), Where N is number of nodes in a tree. This article is contributed by Vishwas Garg. You can Read More →

Counting Sort

Counting sort is a linear time sorting algorithm used to sort items when they belong to a fixed and finite set. Integers which lie in a fixed interval, say k1 to k2, are examples of such items. Algorithm: 1. Count for every key j, 1 ≤ j ≤ m how often it occurs in the Read More →

Sort Elements By Frequency

Problem: You are given an array. You have to sort the array elements in decreasing order of their frequency. eg. [2,3,4,2,8,1,1,2] Output: [2 2 2 1 1 3 4 8] Solution: Method 1 (Use Sorting) 1) Use quick sort algorithm to sort the elements O(nlogn) 2) Scan the sorted array and construct a 2D array Read More →

Check if One String is a Rotation of Other String

Problem: Given two string s1 and s2 how will you check if s1 is a rotated version of s2 ? If s1 = “crazyforcode” then the following are some of its rotated versions: “forcodecrazy” “codecrazyfor” Solution: Steps: First need to check if s1 and s2 are of the same length. Then check, if s2 is Read More →

Find the Minimum Distance Between Two Numbers

Problem: You are given an array and two elements, find the minimum distance between the elements in the array. The array may have duplicates. For example, if the array is (1, 5, 3, 7, 2, 8, 3, 4, 5, 9, 9, 3, 1, 3, 2, 9) Min Distance (4, 7): 4 Min Distance (9, 3): Read More →

Deleting all Leaves from a Binary Tree

Problem: Given a binary tree, how do you remove leaves of it? Solution: By using post-order traversal we can solve this problem (other traversals would also work). Time Complexity: O(n). Where is numbe of nodes in tree. Write or Comment if you find anything incorrect or better approach.

Move the Spaces to Front of the String

Problem: Given a string that has set of words and spaces, write a program to move the spaces to front of string, you need to traverse the array only once and need to adjust the string in place. string = “move these spaces to beginning” output =” movethesepacestobeginning” Solution: 1) Maintain two indices i and Read More →

Find a Pair Whose Sum is Closest to Zero in Array

Problem: This problem is also called minimum absolute sum pair. You are given an array of integers, containing both +ve and -ve numbers. You need to find the two elements such that their sum is closest to zero. eg. Array [15, 5, -20, 30, -45] O/P should be 15, -20 Solution: Searching for two numbers Read More →