Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Chapter 20: Hard

  1. Write a function that adds two numbers. You should not use + or any arithmetic operators.

  2. Write a method to shuffle a deck of cards. It must be a perfect shuffle - in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect.

  3. Write a method to randomly generate a set of m integers from an array of size n. Each element must have equal probability of being chosen.

  4. Write a method to count the number of 2s between 0 and n.

  5. You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. Can you make the searching operation in O(1) time? What about the space complexity for your solution?

  6. Describe an algorithm to find the largest 1 million numbers in 1 billion numbers. Assume that the computer memory can hold all one billion numbers.

  7. Write a program to find the longest word made of other words in a list of words.

EXAMPLE 
Input: test, tester, testertest, testing, testingtester 
Output: testingtester
  1. Given a string s and an array of smaller strings T, design a method to search s for each small string in T.

  2. Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated.

  3. Imagine you have a square matrix, where each cell is filled with either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.

  4. Given an NxN matrix of positive and negative integers, write code to find the submatrix with the largest possible sum.