Deterministic Finite Automaton (DFA) can be used to check whether a number “num” is divisible by “k” or not. If the number is not divisible,… Read More »

Write a function that calculates the day of the week for any particular date in the past or future. A typical application is to calculate… Read More »

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number. For example,… Read More »

Given a number n, write a function that returns count of numbers from 1 to n that don’t contain digit 3 in their decimal representation.… Read More »

Like functions, we can also pass variable length arguments to macros. For this we will use the following preprocessor identifiers. To support variable length arguments… Read More »

Given an array containing only 0s and 1s, find the largest subarray which contain equal no of 0s and 1s. Expected time complexity is O(n).… Read More »

Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples: Input: n… Read More »

Following questions have been asked in GATE CS 2012 exam. 1) The protocol data unit(PDU) for the application layer in the Internet stack is (A)… Read More »

Given a string, a partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, “aba|b|bbabb|a|b|aba” is… Read More »

Following questions have been asked in GATE 2011 CS exam. 1) A thread is usually defined as a ‘light weight process’ because an operating system… Read More »

Given two Binary Search Trees(BST), print the elements of both BSTs in sorted form. The expected time complexity is O(m+n) where m is the number… Read More »

Pattern searching is an important problem in computer science. When we do search for a string in notepad/word file or browser or database, pattern searching… Read More »

Given an array of random numbers. Find longest increasing subsequence (LIS) in the array. I know many of you might have read recursive and dynamic programming… Read More »

In the previous post, we discussed Finite Automata based pattern searching algorithm. The FA (Finite Automata) construction method discussed in previous post takes O((m^3)*NO_OF_CHARS) time.… Read More »

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may… Read More »

The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors. Suppose that we wish… Read More »

Given a Binary Tree, write a function that returns the size of the largest subtree which is also a Binary Search Tree (BST). If the… Read More »

Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but… Read More »

Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as… Read More »

Write a function to connect all the adjacent nodes at the same level in a binary tree. Structure of the given Binary Tree node is… Read More »