Posts

Showing posts from May, 2016

Write the algorithm to print valid ways to print parentheses

Image
Problem Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses. EXAMPLE Input: 3 Output: ( ( ( ) ) ) , (()()), (())(), ()(()), ()()()

Write a method to compute all permutations of a string

Problem Write a method to compute all permutations of a string For example: given an input string, "abc", your program should print the following permutations: a ab abc ac acb b ba bac bc bca c ca cab cb cba

Write a method to return all subsets of a set

Problem Write a method to return all subsets of a set. For example, given set of characters, A = { a, b, c, d} The output should be as blow: ['a', 'ab', 'abc', 'abcd', 'abd', 'ac', 'acd', 'ad', 'b', 'bc', 'bcd', 'bd', 'c', 'cd', 'd']

Finding the magic index in array, A

Image
Problem A magic index in an array A [0. . .n-1] is defined to be an index such that A[i] = i. Given a sorted array of distinct integers, write a method to find a magic index, if one exists, in array A. FOLLOW UP What if the values are not distinct?

Find the ways of climb up the stairs

Problem A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps at a time. Implement a method to count how many possible ways the a child can run up the stairs.

Getting the total covered length given the interval

Problem Write the program that takes the series of intervals and returns a total length covered by the added intervals.  If several intervals intersect, the intersection should be counted only once.  Example:  addInterval(3, 6)  addInterval(8, 9)  addInterval(1, 5)  getTotalCoveredLength() =>; 6  I.e. [1,5) and [3,6) intersect and give a total covered interval [1,6).       [1,6) and [8,9) don't intersect, so the total covered length is a sum of both intervals, that is 5+1=6.                     ______                                        _           _________          0  1  2  3  4  5  6  7  8  9  10      Now, implement the follow...

Sorting the patient's files with three types: Low, Med, High

Problem   An efficient way to sort patient files in an array of just 3 types, "high-importance', 'med-importance', 'low-importance', which are in an arbitrary order(unsorted)   The output preference should start with the highest.   example: [high, low, low, med, high, low]

Finding sub array totaling the given sum (Knapsack variation)

Problem Given the array of integers, A, the target sum, S, and the limit on number of integers to use, K, write the program to return the K integers, whose sum equals, S. Example: A = { 1,2 ,4, 5, 9}, with K = 2, S = 6,  output  = {4, 5}

Shuffling the deck of cards perfectly randomly.

Problem Write the program to shuffle the deck of cards in statistically random order.

Planting flowers with no adjacent flower plots

Image
Problem Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing booleans), return if a given number of new flowers can be planted in it without violating the no-adjacent-flowers rule  Sample inputs  Input: 1,0,0,0,0,0,1,0,0  3 => true  4 => false  Input: 1,0,0,1,0,0,1,0,0  1 => true  2 => false  input: 0  1 => true  2 => false  public boolean canPlaceFlowers(List flowerbed, int numberToPlace) {  // Implementation here  }

Searching the number in the phone book

Problem If I type some numbers in my cell, all phone numbers which have these typed numbers in any order should appear, tell data structure for this.  Example: if I type 926 then  932678....  92678...  9777726....  should appear.  Let me clear it through another example  eg: i enter 321, then  o/p(if they are in book)  9344241..  972153....