设为首页 加入收藏

TOP

C++编程,数据结构,算法类面试题集(6)(一)
2014-11-24 01:26:01 来源: 作者: 【 】 浏览:24
Tags:编程 数据结构 算法 试题集

151. 给你一串数字,让你写程序,把操作符(可以是任意多个 + – * / %)放进各个数字之间,同时还可以在任意位置放进括号,让这个算术表达式的值等于一个给定的数字。比如:给你 5 3 8 9 = 6你的程序应该输出 5 * (4 + 8) % 9 = 6


CODE, 24


152. 一道编程题,大意是给定一个类read1,它有一个函数read4096,每次调用它可以从文件中读取4K个字节,同时移动文件指针4K个位置(若文件中剩余数据不足4K,则读取剩下的所有数据),这个函数返回实际读取的字节数,int型;要求实现另一个类read2中的一个函数read,它有一个参数int n_byte,这个函数可以从文件中读取由n_byte指定的字节数,同样返回实际读取的字节数;然后又给出一个函数reset,它可以将文件指针重置到起始位置,要求实现read2中的另一个函数seek,有一个参数int pos
,它可以将缓冲区的指针移动到第pos个字节的位置,返回实际指针移动到的位置。可以在read2中添加任意变量来完成这两个函数。


多次做read4096即可,为了加速,可以重用上次seek的结果



153. 编程题问的是boggle游戏的问题:给定一个4*4的矩阵,每个位置有一个字母,可以从一个位置跳转到周围八个相邻位置中的任何一个,但不能跳到已经访问过的位置,要求找出所有的单词(假设给定了一个词典)。http://en.wikipedia.org/wiki/Boggle


构造一个单词的prefix字典,然后再递归+回溯。。。



154. Find median for k sorted arrays


设k个sorted array为a1, a2, …, ak.


先找出这k个sorted array的median, m1, m2, … mk.


再找出这k个median的median: mm


然后可以把所有比mm小的数和大的数都去掉,大致有一半


然后再找剩下的数的median(递归),复杂度O(klogn)



155. “Count and Say problem” Write a code to do following:
n String to print
0 1
1 1 1
2 2 1
3 1 2 1 1

Base case: n = 0 print “1″
for n = 1, look at previous string and write number of times a digit is seen and the digit itself. In this case, digit 1 is seen 1 time in a row… so print “1 1″
for n = 2, digit 1 is seen two times in a row, so print “2 1″
for n = 3, digit 2 is seen 1 time and then digit 1 is seen 1 so print “1 2 1 1″
for n = 4, you will print “1 1 1 2 2 1″


Consider the numbers as integers for simplicity. e.g. if previous string is “10 1″ then the next will be “1 10 1 1″ and the next one will be “1 1 1 10 2 1″ 10


CODE



156. 给定一个数组, A[ 0 .... N ]作为输入数组。给定一个函数 f(i,j) ,这个函数以两个下表(i,j)为输入, 返回一个值。(这个函数是个blackbox, 唯一的信息就是输入两个整数返回一个值)。要求把数组A分为3份,使得 f(0,a) + f(a,b) + f(b,N)最小。
LEARN



157. nm的字符矩阵。然后给你一个字符串。问是否可以在矩阵中找到他的tracktrack是指从其
中一个符出发,可以向四周走,可以重复,可以回头。
e.g:
a b
c d
string: ‘bdba’ could be found but not for ‘bcd’.


递归+回溯,也可以用DP优化



158. Given three linked list of integers, all sorted. Find the first shared integer in all three. What is the time complexity


略。。。



159. Initially you have a 2×2 matrix, say zoom1:
a b
c d
zooming it results in a 4×4 matrix (zoom2) as follows:
aa ab ba bb
ac ad bc bd
ca cb da db
cc cd dc dd
zooming it again will result in an 8×8 matrix and so on..
aaaa aaab abaa abab baba babb bbba bbbb
aaac aaad abac abad babc babd bdba bdbb
acaa acab adaa adab bcba bcbb bdba bdbb
acac acad adac adad bcbc bcbd bdbc bdbd
caca cacb cbca cbcb dada dadb dbda dbda
cacc cacd cbcc cbcd dadc dadd dbdc dbdd
ccca cccb cdca cdcb dcda dcdb ddda dddb
cccc cccd cdcc cdcd dcdc dcdd dddc dddd
The question is, given a sequence say abaaccda… we need to find out the
sequence coming just left to it. For e.g. if the given sequence is “bd” for
zoom2, the sequence coming just left to it is “bc”. For “cd” it’s “cc” etc.


CODE



160. 如何在binary tree找一个path root leaf, 和是sum
2)
如何序列化一个binary tree到一个文件
3)
如果有一个已经序列化的tree, 很大,要做1)的算法,怎么做,2)中如果有多个方法选择哪中序列化的方法比较好?
4
如果有1000w个已经序列化的小文件,对他们都要做3),如何提高性能,系统是5台机器


TO LEARN



161. Programming: interval halving. Given a continuous function ‘f(x)’ and an interval on the x-axis from ‘start’ to ‘end’. It is know that ‘f(x)=0′ for exactly one value of ‘x’ between ‘start’ and ‘end’, and that ‘f(x)’ crosses the x-axis at this point. Write a program that repeatedly cuts in half the interval until the interval containing ‘f(x)=0′ is equal or less than ‘epsilon’ wide.


略。。。



162 [Facebook] You are given N ranges of date offsets when N employees are present in an organization. Something like
1-4 (i.e. employee will come on 1st, 2nd, 3rd and 4th day )
2-6
8-9
..
1-14
You have to organize an event on mi

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇.net程序员面试试题(一) 下一篇C++编程,数据结构,算法类面试题..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: