`

非常有用的101道算法部分常见面试题

阅读更多

1. Given a rectangular (cuboidal for the puritans) cake with a rectangular piece removed (any size or orientation), how would you cut the remainder of the cake into two equal halves with one straight cut of a knife ? <o:p>

2. You're given an array containing both positive and negative integers and required to find the sub-array with the largest sum (O(N) a la KBL). Write a routine in C for the above. <o:p>

3. Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like. [ I ended up giving about 4 or 5 different solutions for this, each supposedly better than the others ]. <o:p>

4. Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all. [ This one had me stuck for quite some time and I first gave a solution that did have floating point computations ]. <o:p>

5. Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal. [ I gave the obvious solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn't too pleased and asked me to give a solution which didn't need the array ]. <o:p>

6. Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.] <o:p>

7. Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it. <o:p>

8. How many points are there on the globe where by walking one mile south, one mile east and one mile north you reach the place where you started. <o:p>

9. Give a very good method to count the number of ones in a "n" (e.g. 32) bit number. <o:p>

ANS. Given below are simple solutions, find a solution that does it in log (n) steps. <o:p>

Iterative<o:p>

function iterativecount (unsigned int n)<o:p>

begin<o:p>

int count=0;<o:p>

while (n)<o:p>

begin<o:p>

count += n & 0x1 ;<o:p>

n >>= 1;<o:p>

end<o:p>

return count;<o:p>

end<o:p>

Sparse Count<o:p>

function sparsecount (unsigned int n)<o:p>

begin<o:p>

int count=0;<o:p>

while (n)<o:p>

begin<o:p>

count++;<o:p>

n &= (n-1);<o:p>

end<o:p>

return count ;<o:p>

end<o:p>

10. What are the different ways to implement a condition where the value of x can be either a 0 or a 1. Apparently the if then else solution has a jump when written out in assembly. if (x == 0) y=a else y=b There is a logical, arithmetic and a data structure solution to the above problem. <o:p>

11. Reverse a linked list. <o:p>

12. Insert in a sorted list <o:p>

13. In a X's and 0's game (i.e. TIC TAC TOE) if you write a program for this give a fast way to generate the moves by the computer. I mean this should be the fastest way possible. <o:p>

The answer is that you need to store all possible configurations of the board and the move that is associated with that. Then it boils down to just accessing the right element and getting the corresponding move for it. Do some analysis and do some more optimization in storage since otherwise it becomes infeasible to get the required storage in a DOS machine. <o:p>

14. I was given two lines of assembly code which found the absolute value of a number stored in two's complement form. I had to recognize what the code was doing. Pretty simple if you know some assembly and some fundaes on number representation. <o:p>

15. Give a fast way to multiply a number by 7. <o:p>

16. How would go about finding out where to find a book in a library. (You don't know how exactly the books are organized beforehand). <o:p>

17. Linked list manipulation. <o:p>

18. Tradeoff between time spent in testing a product and getting into the market first. <o:p>

19. What to test for given that there isn't enough time to test everything you want to. <o:p>

20. First some definitions for this problem: a) An ASCII character is one byte long and the most significant bit in the byte is always '0'. b) A Kanji character is two bytes long. The only characteristic of a Kanji character is that in its first byte the most significant bit is '1'. <o:p>

Now you are given an array of a characters (both ASCII and Kanji) and, an index into the array. The index points to the start of some character. Now you need to write a function to do a backspace (i.e. delete the character before the given index). <o:p>

21. Delete an element from a doubly linked list. <o:p>

22. Write a function to find the depth of a binary tree. <o:p>

23. Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted. <o:p>

24. Assuming that locks are the only reason due to which deadlocks can occur in a system. What would be a foolproof method of avoiding deadlocks in the system. <o:p>

25. Reverse a linked list. <o:p>

Ans: Possible answers - <o:p>

iterative loop
curr->next = prev;
prev = curr;
curr = next;
next = curr->next
endloop

recursive reverse(ptr)
if (ptr->next == NULL)
return ptr;
temp = reverse(ptr->next);
temp->next = ptr;
return ptr;
end
<o:p>

26. Write a small lexical analyzer - interviewer gave tokens. expressions like "a*b" etc. <o:p>

27. Besides communication cost, what is the other source of inefficiency in RPC? (answer : context switches, excessive buffer copying). How can you optimize the communication? (ans : communicate through shared memory on same machine, bypassing the kernel _ A Univ. of Wash. thesis) <o:p>

28. Write a routine that prints out a 2-D array in spiral order! <o:p>

29. How is the readers-writers problem solved? - using semaphores/ada .. etc. <o:p>

30. Ways of optimizing symbol table storage in compilers. <o:p>

31. A walk-through through the symbol table functions, lookup() implementation etc. - The interviewer was on the Microsoft C team. <o:p>

32. A version of the "There are three persons X Y Z, one of which always lies".. etc.. <o:p>

33. There are 3 ants at 3 corners of a triangle, they randomly start moving towards another corner.. what is the probability that they don't collide. <o:p>

34. Write an efficient algorithm and C code to shuffle a pack of cards.. this one was a feedback process until we came up with one with no extra storage. <o:p>

35. The if (x == 0) y = 0 etc.. <o:p>

36. Some more bitwise optimization at assembly level <o:p>

37. Some general questions on Lex, Yacc etc. <o:p>

38. Given an array t[100] which contains numbers between 1..99. Return the duplicated value. Try both O(n) and O(n-square). <o:p>

39. Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array. <o:p>

40. Given a sequence of characters. How will you convert the lower case characters to upper case characters. ( Try using bit vector - solutions given in the C lib -typec.h) <o:p>

41. Fundamentals of RPC. <o:p>

42. Given a linked list which is sorted. How will u insert in sorted way. <o:p>

43. Given a linked list How will you reverse it. <o:p>

44. Give a good data structure for having n queues ( n not fixed) in a finite memory segment. You can have some data-structure separate for each queue. Try to use at least 90% of the memory space. <o:p>

45. Do a breadth first traversal of a tree. <o:p>

46. Write code for reversing a linked list. <o:p>

47. Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9). <o:p>

48. Given an array of integers, find the contiguous sub-array with the largest sum. <o:p>

ANS. Can be done in O(n) time and O(1) extra space. Scan array from 1 to n. Remember the best sub-array seen so far and the best sub-array ending in i. <o:p>

49. Given an array of length N containing integers between 1 and N, determine if it contains any duplicates. <o:p>

ANS. [Is there an O(n) time solution that uses only O(1) extra space and does not destroy the original array?] <o:p>

50. Sort an array of size n containing integers between 1 and K, given a temporary scratch integer array of size K. <o:p>

ANS. Compute cumulative counts of integers in the auxiliary array. Now scan the original array, rotating cycles! [Can someone word this more nicely?] <o:p>

* 51. An array of size k contains integers between 1 and n. You are given an additional scratch array of size n. Compress the original array by removing duplicates in it. What if k << n? <o:p>

ANS. Can be done in O(k) time i.e. without initializing the auxiliary array! <o:p>

52. An array of integers. The sum of the array is known not to overflow an integer. Compute the sum. What if we know that integers are in 2's complement form? <o:p>

ANS. If numbers are in 2's complement, an ordinary looking loop like for(i=total=0;i< n;total+=array[i++]); will do. No need to check for overflows! <o:p>

53. An array of characters. Reverse the order of words in it. <o:p>

ANS. Write a routine to reverse a character array. Now call it for the given array and for each word in it. <o:p>

* 54. An array of integers of size n. Generate a random permutation of the array, given a function rand_n() that returns an integer between 1 and n, both inclusive, with equal probability. What is the expected time of your algorithm? <o:p>

ANS. "Expected time" should ring a bell. To compute a random permutation, use the standard algorithm of scanning array from n downto 1, swapping i-th element with a uniformly random element <= i-th. To compute a uniformly random integer between 1 and k (k < n), call rand_n() repeatedly until it returns a value in the desired range. <o:p>

55. An array of pointers to (very long) strings. Find pointers to the (lexicographically) smallest and largest strings. <o:p>

ANS. Scan array in pairs. Remember largest-so-far and smallest-so-far. Compare the larger of the two strings in the current pair with largest-so-far to update it. And the smaller of the current pair with the smallest-so-far to update it. For a total of <= 3n/2 strcmp() calls. That's also the lower bound. <o:p>

56. Write a program to remove duplicates from a sorted array. <o:p>

ANS. int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
} else
current++;

return insert;

}
<o:p>

 <o:p>

57. C++ ( what is virtual function ? what happens if an error occurs in constructor or destructor. Discussion on error handling, templates, unique features of C++. What is different in C++, ( compare with unix). <o:p>

58. Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list). <o:p>

59. Given 3 lines of assembly code : find it is doing. IT was to find absolute value. <o:p>

60. If you are on a boat and you throw out a suitcase, Will the level of water increase. <o:p>

61. Print an integer using only putchar. Try doing it without using extra storage. <o:p>

62. Write C code for (a) deleting an element from a linked list (b) traversing a linked list <o:p>

63. What are various problems unique to distributed databases <o:p>

64. Declare a void pointer ANS. void *ptr; <o:p>

65. Make the pointer aligned to a 4 byte boundary in a efficient manner ANS. Assign the pointer to a long number and the number with 11...1100 add 4 to the number <o:p>

66. What is a far pointer (in DOS) <o:p>

67. What is a balanced tree <o:p>

68. Given a linked list with the following property node2 is left child of node1, if node2 < node1 else, it is the right child. <o:p>

          O P<o:p>

|<o:p>

|<o:p>

O A<o:p>

|<o:p>

|<o:p>

O B<o:p>

|<o:p>

|<o:p>

O C<o:p>

How do you convert the above linked list to the form without disturbing the property. Write C code for that. <o:p>

                               O P<o:p>

|<o:p>

|<o:p>

O B<o:p>

/ \<o:p>

/   \<o:p>

/     \<o:p>

O ?     O ?<o:p>

determine where do A and C go <o:p>

69. Describe the file system layout in the UNIX OS <o:p>

ANS. describe boot block, super block, inodes and data layout <o:p>

70. In UNIX, are the files allocated contiguous blocks of data <o:p>

ANS. no, they might be fragmented <o:p>

How is the fragmented data kept track of <o:p>

ANS. Describe the direct blocks and indirect blocks in UNIX file system <o:p>

71. Write an efficient C code for 'tr' program. 'tr' has two command line arguments. They both are strings of same length. tr reads an input file, replaces each character in the first string with the corresponding character in the second string. eg. 'tr abc xyz' replaces all 'a's by 'x's, 'b's by 'y's and so on. ANS.
a) have an array of length 26.
put 'x' in array element corr to 'a'
put 'y' in array element corr to 'b'
put 'z' in array element corr to 'c'
put 'd' in array element corr to 'd'
put 'e' in array element corr to 'e'
and so on.

the code
while (!eof)
{
c = getc();
putc(array[c - 'a']);
}
<o:p>

72. what is disk interleaving <o:p>

73. why is disk interleaving adopted <o:p>

74. given a new disk, how do you determine which interleaving is the best a) give 1000 read operations with each kind of interleaving determine the best interleaving from the statistics <o:p>

75. draw the graph with performance on one axis and 'n' on another, where 'n' in the 'n' in n-way disk interleaving. (a tricky question, should be answered carefully) <o:p>

76. I was a c++ code and was asked to find out the bug in that. The bug was that he declared an object locally in a function and tried to return the pointer to that object. Since the object is local to the function, it no more exists after returning from the function. The pointer, therefore, is invalid outside. <o:p>

77. A real life problem - A square picture is cut into 16 squares and they are shuffled. Write a program to rearrange the 16 squares to get the original big square. <o:p>

78.
int *a;
char *c;
*(a) = 20;
*c = *a;
printf("%c",*c);

what is the output?
<o:p>

79. Write a program to find whether a given m/c is big-endian or little-endian! <o:p>

80. What is a volatile variable? <o:p>

81. What is the scope of a static function in C ? <o:p>

82. What is the difference between "malloc" and "calloc"?

分享到:
评论
1 楼 resintwo 2008-01-09  

相关推荐

    非常有用的101道算法部分常见面试题(英文).txt

    根据给定文件的信息,我们可以提炼出以下IT领域的关键知识点,主要围绕...以上问题不仅涵盖了算法设计、数据结构优化、位操作等技术点,还涉及到了面试技巧和编程实践,是IT行业尤其是软件开发领域中常见的考察内容。

    非常有用的101道算法部分常见面试题(面试题目荟萃)

    常见算法面试题汇总 以下是对给定文件信息的知识点总结: 问题1: cake 分割 如何将一个矩形蛋糕(或立方体)分割成两个相等的部分,假设已经有一块矩形蛋糕被移除?这道题考验了候选人的问题分析和解决能力。 ...

    java常见面试题(史上最全最经典-希望对你有用)

    Java常见面试题 Java是最流行的编程语言之一,掌握Java的基础知识是非常重要的。在这里,我们总结了Java常见的面试题,涵盖了Java的基础部分,包括基本语法、类相关的语法、内部类的语法、继承相关的语法、异常的...

    力扣算法-互联网公司常见算法面试题

    力扣(LeetCode)是一个非常受欢迎的在线编程挑战平台,专为准备算法面试而设计,尤其在互联网行业中广泛被用作考察技术候选人能力的标准。它涵盖了各种数据结构和算法问题,帮助开发者提升解决实际问题的能力。这个...

    常见算法介绍、算法刷题(含解析与代码)、笔试面试算法题文档总结.docx

    ### 常见算法知识点详解 #### 排序算法 - **冒泡排序**:这是一种简单直观的排序方法。它通过重复地遍历列表,比较每一对相邻元素,并在必要时交换它们的位置来工作。每次遍历都会把当前未排序部分的最大值移到...

    C++程序员应聘常见面试题

    我收集的一些C++程序员试题,部份有答案。建议面试相关工作前先了解一下。由于本人是从事游戏开发的,所以部分题是与游戏程序开发相关的,如DirectX,游戏引擎和算法,不过这些只占小部分。希望这些资料对大家有用!

    前端面试题之算法剑指offer题集.zip

    10. **计数与概率**:计数问题如组合、排列、鸽巢原理等,概率问题如蒙特卡洛方法,这些在某些特定的面试题中也会出现。 在准备前端面试时,不仅要理解和掌握这些算法,还需要通过实际编写代码来锻炼编程技巧。同时...

    各大软件公司面试--算法笔试题

    在准备各大软件公司的面试时,算法笔试题是不可或缺的一...通过反复练习和研究各大软件公司的面试题,可以提升你的算法功底,提高面试成功率。记住,理论知识和实践经验相结合,才能在竞争激烈的软件行业中脱颖而出。

    面试算法题及技巧.zip

    在准备面试时,掌握算法题和解题...以上只是部分可能涵盖的知识点,实际“面试算法题及技巧.zip”中可能还包含更多具体题目和解题策略。通过深入学习和练习,你将能够更好地应对面试中的算法挑战,提升自己的竞争力。

    java面试题应该有用的额java面试题java面试题java面试题java面试题

    Java面试题是每个Java开发者在求职过程中必须面对的挑战,涵盖范围广泛,涉及语言基础、数据结构、算法、多线程、JVM优化、框架应用等多个方面。以下是一些可能出现的Java面试知识点详解: 1. **Java语言基础**:这...

    Java经典算法50题——答案下载!

    单链表、双链表和环形链表都是常见的链表类型,在解决复杂问题时非常有用。 3. 栈与队列:栈遵循“后进先出”(LIFO)原则,常用于表达式求值、递归等问题;队列遵循“先进先出”(FIFO)原则,适用于任务调度和多...

    程序员面试题-多人面试经验,一定有用!

    2. **算法与数据结构**:基础的排序算法(如冒泡、选择、插入、快速、归并排序)、查找算法(如二分查找)、树结构(如二叉树、平衡树AVL和红黑树)、图算法(如Dijkstra、Floyd、Kruskal)都是常见的面试题。...

    2021最新Java面试题及答案V2.0.pdf

    以上知识点涵盖了Java面试中常见的问题,对于准备Java面试的求职者来说,深入理解这些知识点是很有帮助的。通过系统复习这些内容,面试者可以更好地应对面试官的提问,展现出自己的技术实力和专业素养。

    C#最新面试题(127道)word格式

    ### C#面试题详解 #### 1. 访问修饰符的理解 - **Private**: 这个修饰符定义了一个私有成员,意味着只有定义它的类内部可以访问这个成员。这通常用于封装类的内部状态,确保外部代码无法直接修改这些状态。 - **...

    算法导论第三版课后答案

    动态规划和贪心策略在这里非常有用。 5. 理论分析:有时,习题会考察算法的性能界限,比如计算二分查找的平均和最坏情况的时间复杂度。 6. 图论问题:例如,解决旅行商问题、最小生成树的Kruskal算法或Prim算法。...

    算法演示flash,很多算法题,都是有flash版的

    这对于初学者来说特别有用,因为它们能够直观地看到算法如何运作,而不仅仅是阅读代码或理论描述。 1. **动态规划**:动态规划是一种用于求解最优化问题的方法,通常涉及分治策略。在Flash演示中,可能会展示如何...

    .NET经典面试题

    以上就是.NET面试中常见的部分知识点,包括访问修饰符、页面间数据传递、递归、委托与事件、方法重写、变量传递、控件操作、排序算法、索引器以及数列求和等。掌握这些知识点对于理解和开发.NET应用程序至关重要。

    算法题的概要介绍与分析

    - **Competitive-Programming-Docs**:这是一个非常全面的资源集合,包括了算法竞赛相关的论文、课件、文档、笔记以及平台链接等资料,对于那些希望进一步提高自己算法竞赛水平的学习者来说,非常有用。 #### 书籍...

    嵌入式开发经典网页收藏.rar

    “非常有用的101道算法部分常见面试题”揭示了算法在嵌入式开发中的重要性。无论是解决实时问题还是处理复杂的数据,算法都是解决问题的关键。这101道题目可能包括排序、搜索、图论等基础算法,也可能会涉及数据结构...

    微软面试题 JAVA方面 面试前看看很有用

    【标题】:“微软面试题 JAVA方面 面试前看看很有用” 【描述】:“面试成功 现在面试很大一部分都是考理论方面跟做几个小程序就行 所以考前看一下 对你进入公司帮助很大的” 从这个标题和描述中,我们可以提炼出...

Global site tag (gtag.js) - Google Analytics