摘要: Given an array of size n, find all the possible sub set of the array of size k(all the subsets must be of size k).Q:给一个大小为n的数组,输出其中k个数字的组合。A:void subarray(int arr[], int t[], int n, int index, int k, int kIndex) { int i; if (n == 0) return; if (kIndex == k) { // 说明已经取到了k个数字,打印出来 ... 阅读全文
posted @ 2011-12-19 14:16 百分百好牛 阅读(325) 评论(0) 推荐(0) 编辑
摘要: You are given N ranges of date offsets when N employees are present in an organization. Something like1-4 (i.e. employee will come on 1st, 2nd, 3rd and 4th day )2-68-9..1-14You have to organize an event on minimum number of days such that each employee can attend the event at least twice.Q:你有N个员工的作息 阅读全文
posted @ 2011-12-15 11:48 百分百好牛 阅读(375) 评论(0) 推荐(0) 编辑
摘要: 有个enum叫做Color {Red, White, Blue}, 一个神奇的method叫Color getColor(int)能把int value对应到Color上. 然后给一个int array, 要求sort the array to theorder Red, White then Blue. 只能用constant memory. 如果两个数字都对应一种颜色,这两个数字随便怎么排. 此题我觉得是整个面试中最tricky的一道了吧. 提示是如果只有两种颜色如何sort.updated:以前看到这道题,完全没有想法。今天又看到它,觉得本质上,这是一个类似dutch flag part 阅读全文
posted @ 2011-12-01 16:19 百分百好牛 阅读(202) 评论(0) 推荐(0) 编辑
摘要: Suppose there is a circle. You have five points on that circle. Each point corresponds to a petrol pump. You are given two sets of data.1. The amount of petrol that petrol pump will give.2. Distance from that petrol pump tp the next petrol pump.(Assume for 1 lit Petrol the truck will go 1 km) Now ca 阅读全文
posted @ 2011-11-30 16:51 百分百好牛 阅读(209) 评论(0) 推荐(0) 编辑
摘要: Given a string consisting of a,b and c's, we can perform the following operation: Take any two adjacent distinct characters and replace it with the third character. For example, if 'a' and 'c' are adjacent, they can replaced with 'b'. What is the smallest string which can 阅读全文
posted @ 2011-11-25 15:04 百分百好牛 阅读(270) 评论(0) 推荐(0) 编辑
摘要: You are given a word and a dictionary. Now propose an algorithm editthe word (insert / delete characters) minimally to get a word thatalso exists in the dictionary. Cost of insertion and deletion is same.Write pseudocode for it.Seems like minimum edit distance problem but some modification isneeded. 阅读全文
posted @ 2011-11-25 11:07 百分百好牛 阅读(229) 评论(0) 推荐(0) 编辑
摘要: Given a number,find the next higher number using the same digits in the number.Eg- 15432, Soln- 21345.A:1. 从右向左扫描数字,找到a[i]>a[i - 1], 记此时的i为flag;2. v = a[i - 1],然后从i开始向后扫描,找到Min(a[j] > v), i <= j <=n-1的值,将之与a[i - 1]交换3. 然后对从 i 到 (n-1) (即i后面的所有数字)按从小到大的顺序排序,即生成目标数 阅读全文
posted @ 2011-11-25 10:29 百分百好牛 阅读(231) 评论(0) 推荐(0) 编辑
摘要: Find the substring of length 3 which is present in the reverse order from the string.Ex: if the string is abcdcba (cba is the reverse of abc) so we should return cba.And was asked to improve upon the complexity.A:1. 遍历字符串,取长度为3的子字符串,然后生成hash表1;2. 反转字符串先;3. 再遍历字符串,取长度为3的子字符串,得到hash值,然后查表1;时间复杂度:O(n) 阅读全文
posted @ 2011-11-24 13:48 百分百好牛 阅读(175) 评论(0) 推荐(0) 编辑
摘要: Given an integer find the next(smallest number greater than given number) integer which is palindromfor example:111 next palindrom 121301 next palindrom 303A: 设原始数字为a,数字位数是n1.如果数字的位数n是odd奇数,固定中间的数字;如果是even偶数,固定中间的两个数字;2. 将固定的数字前面的字符串,反转复制到后面去,生成数字b;e.g: 12345 -> 12321, 123456 - > 1234213. 判断新生 阅读全文
posted @ 2011-11-18 16:48 百分百好牛 阅读(508) 评论(0) 推荐(0) 编辑
摘要: Let's say you have a phrase without any spaces - eg. "thisisawesome". Given a dictionary, how would you add spaces in this string?我的理解是:把这个字符串用空格拆成字符 "this" "is" "awesome",这三个字符应该都能够在字典里面找到。比如,还可以这样拆"thisi" "saw" "esome", 这里我假 阅读全文
posted @ 2011-11-17 22:31 百分百好牛 阅读(291) 评论(0) 推荐(0) 编辑