Posts

Showing posts with the label Cracking Coding Interview

Finding the element in the M X N matrix sorted in ascending order

Problem Given an M $\times$ N matrix in which each row and each column are sorted in ascending order, write a method to find an element.

Finding the string in the sorted array of strings with empty strings

Problem Given a sorted array of strings that is interspersed with empty strings, write a method to find the location of a given string. Example input: find "ball" in  {"at", "", "", "","ball", "", "", "car", "", "". "dad", "",""} Output: 4

Sorting the strings in 20GB file with one string in each line.

Problem Imagine you have a 20 GB file with one string per line. Explain how you would sort strings in the file.

Merge two sorted array A and B, where A has larger buffer to hold B at the end of it.

Problem You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write the algorithm to merge A and B without allocation additional memory.

Write the function to count the number of ways of parenthesizing the expression

Problem Given a boolean expression consisting of the symbols 0,1, &, |, and ^, and a desired boolean result value result, implement a function to count the number of ways of parenthesizing the expression such that it evaluates to result. Example) Expression: 1^0|0|1 Desired result: false (0) Output: 2 ways. 1^( (0|0)|1) and 1^(0|(0|1)).

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.

Searching number in the circular array [reviewed]

Problem Given the circular array of sorted values, search the specified value. For example, A = { 4,5,6, 1,2,3}, target = 1; output  = true Your algorithm should be better than O(N).