# Archive for July 2018

Problem statement Given a matrix in which each row and each column is sorted, write a method to find an element in it. Solution Assumptions: 1. Rows are sorted left to right in ascending order. Columns are sorted top to bottom in ascending order. 2. Matrix is of size . . . . Read more

Problem Statement Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents Solution This is a recursive problem, so let’s figure out how to do makeChange(n) using prior solutions (i . . . Read more

Problem Statement Implement an algorithm to print all valid (e g , properly opened and closed) combinations of n-pairs of parentheses Example Input: 3 (e g , 3 pairs of parentheses) Output: ()()(), ()(()), (())(), ((())) Solution public class PrintAllValidParens { static void solve(int l, int r, int i, char[] . . . Read more

Problem Statement Write a method to compute all permutations of a string Solution Let’s assume a given string represented by the letters To permute set , we can select the first character, , permute the remainder of the string to get a new list Then, with that new list, we . . . Read more

Problem statement You are given two 32-bit numbers, and , and two bit positions, and . Write a method to set all bits between and in equal to (e g , becomes a substring of located at and starting at ) Example Input: , , , Output: Solution public class . . . Read more

Problem Statement Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height. Solution We will try to create a binary tree such that for each node, the number of nodes in the left subtree and the right subtree are equal, if possible Algorithm: . . . Read more