# Monthly Archive: October 2015

## Generate all combinations of an array

Input : [ 1, 2, 3 ]   Output : [  ‘ ‘,  1,  2,  12,  3,  13,  23,  123  ]   Logic : There are 2^n possible combinations for the array of...

## Implement a power function

Naive logic: Loop through the number of times the exponent, and during each iteration multiply the base If our exponent is 10,000 then our loop has to run 10,000 times. But we can optimize...

## Determine given a pattern and string are isomorphic

Problem : Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and...

## Determine if given strings are isomorphic

Problem : Given two (dictionary) words as Strings, determine if they are isomorphic. Two words are called isomorphic if the letters in one word can be remapped to get the second word. Remapping a...

## Find consecutive segments in an sorted array

Problem : Number list compressing. Given an sorted array. Input: sorted number list [ 1, 2, 3,10, 25, 26, 30, 31, 32, 33] Output: find consecutive segments print: 1-3, 10, 25-26, 30-33 Logic :...

## Rearrange characters in a string so that no character repeats consecutively.

Below are some of the test strings with their expected output. Input: aaabc Output: abaca Input: aa Output: No valid output Input: aaaabc Output: No valid output Logic : Create frequency map of the...

## Withdraw all money from bank account

Problem : You have some money in your bank account, the only function to withdraw money is Withdraw(value), if the value is greater than the money you have it returns 0, otherwise it withdraws...

## Print half pyramids using * as shown

Pattern 1 : * ** *** **** ***** Logic : i height Number of * (= i) 1 5 1 2 5 2 3 5 3 4 5 4 5 5 5 Solution :...

## Create a minimal height binary search tree from given sorted array of unique integers

Problem description : Given a sorted (increasing order) array with unique integer elements, create a binary search tree (BST) with minimal height. Input : A sorted array with unique integer elements // [0, 1,...

## Determine if a binary tree is balanced

A balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ more than one. Input : The root of a binary tree Output...