Вы находитесь на странице: 1из 3

1) Write a program to sort a stack in ascending order.

You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to write this program: push | pop | peek | isEmpty. 2) input: "kitten%20pic.jpg" output: "kitten pic.jpg" %20 -> ' ' %3A -> '?' %3D -> ':' modify your input in place. no string library functions. void DecodeURL(string str) 3) Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation. 4) An array A[1...n] contains all the integers from 0 to n except for one number which is missing.In this problem, we cannot access an entire integer in A with a single operation.The elements of A are represented in binary, and the only operation we can use to access them is fetch the jth bit of A[i], which takes constant time.Write code to find the missing integer.Can you do it in O(n) time? 5) Write a method to sort an array of strings so that all the anagrams are next to each other. 6) If you have a 2 GB file with one string per line, which sorting algorithm would you use to sort the file and why? 7) A circus is designing a tower routine consisting of people standing atop one anothers shoulders.For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her.Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower. EXAMPLE: Input (ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110) Output: The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190) 8) Write an algorithm which computes the number of trailing zeros in n factorial. 9) Given an integer between 0 and 999,999, print an English phrase that describes the integer (eg, One Thousand, Two Hundred and Thirty Four).

10) Design a method to find the frequency of occurrences of any given word in a book. 11) Write a method to count the number of 2s between 0 and n. 12) 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? 13) 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 14) 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. 15) Given n strings. One string can be connected to other only if last letter of first string is same as first letter of second string. Like this all n strings are connected. Find whether it is possible to form a chain of these strings i.e is it possible to connect all the strings in such a way that all the strings are connected and each string occurs exactly once.

16) Write an algorithm to print a binary tree level wise and that too from leaves to root. Also only one queue and one stack can be used for the purpose. 17) Add arithmetic operators (plus, minus, times, divide) to make the following expression true: 3 1 3 6 = 8.You can use any parentheses youd like.

18) (microsoft) A bunch of men are on an island. A genie comes down and gathers everyone together and places a magical hat on some peoples heads (i.e., at least one person has a hat). The hat is magical: it can be seen by other people, but not by the wearer of the hat himself. Toremove the hat, those (and only those who have a hat) must dunk themselves underwater at exactly midnight. If there are n people and c hats, how long does it take the men to remove the hats? The men cannot tell each other (in any way) that they have a hat. FOLLOW UP Prove that your solution is correct. 19) There are one hundred closed lockers in a hallway.A man begins by opening all one hundred lockers.Next, he closes every second locker.Then he goes to every third locker and closes it if it is open or opens it if it is closed (e.g., he toggles every thirdlocker).After

his one hundredth pass in the hallway, in which he toggles only locker number one hundred, how many lockers are open? 20) Given a directed graph, design an algorithm to find out whether there is a route between two nodes

Вам также может понравиться