IT公司面试题

IBM社会招聘笔试题

  1. 一个粗细均匀的长直管子,两端开口,里面有4个白球和4个黑球,球的直径、两端开口的直径等于管子的内径,现在白球和黑球的排列是wwwwbbbb,要求不取出任何一个球,使得排列变为bbwwwwbb。
    把管子两端接起来形成一个通环,将黑球从接口挤过去两个
  2. 一只蜗牛从井底爬到井口,每天白天蜗牛要睡觉,晚上才出来活动,一个晚上蜗牛可以向上爬3尺,但是白天睡觉的时候会往下滑2尺,井深10尺,问蜗牛几天可以爬出来? 8
  3. 在一个平面上画1999条直线最多能将这一平面划分成多少个部分?
    1999001
  4. 在太平洋的一个小岛上生活着土人,他们不愿意被外人打扰,一天,一个探险家到了岛上,被土人抓住,土人的祭司告诉他,你临死前还可以有一个机会留下一句话,如果这句话是真的,你将被烧死,是假的,你将被五马分尸,可怜的探险家如何才能活下来?
    我将被五马分尸
  5. 怎样种四棵树使得任意两棵树的距离相等。
    种山上,等边三角锥
  6. 27个小运动员在参加完比赛后,口渴难耐,去小店买饮料,饮料店搞促销,凭三个空瓶可以再换一瓶,他们最少买多少瓶饮料才能保证一人一瓶?
    19
  7. 有一座山,山上有座庙,只有一条路可以从山上的庙到山脚,每周一早上8点,有一个聪明的小和尚去山下化缘,周二早上8点从山脚回山上的庙里,小和尚的上下山的速度是任意的,在每个往返中,他总是能在周一和周二的同一钟点到达山路上的同一点。例如,有一次他发现星期一的8点30和星期二的8点30他都到了山路靠山脚的3/4的地方,问这是为什么?
    那时肯定的,因为上下山只有一条路
  8. 有两根不均匀分布的香,每根香烧完的时间是一个小时,你能用什么方法来确定一段15分钟的时间?
    把第一根一头点着,同时把第二根两头都点着,在第二根燃尽的同时点燃第一根的另一头,这时开始计时,至第一根燃尽就是十五分钟

IBM面试题目

  1. Describe your greatest achievement in the past 4-5 years?

  2. What are your short & long term career objectives? What do you think is the most ideal job for you?

  3. Why do you want to join IBM? What do you think you can contribute to IBM?

Google 中国笔试题目

1.1关于IP协议那个正确
A IP是TCP上层协议B IP协议是应用层协议C由于两个属于同一层协议,他们之间可以直接通信DIP协议不提供可靠的通信
1.2 关于内存正确的是
A内存的存取速度不能低于cpu速度,否则会造成数据丢失
B程序只有在数据和代码等被调入内存后才能运行
C采用虚拟内存技术后程序可以在硬盘上直接运行
D某计算机的内存容量为16MB,那么他的地址总线为24位
1.3单链表中结点的结构为(data,link),若想删除结点p(不是头节点或者尾结点)的直接后继,则应执行下列哪个操作
A p=p->link ; p->link=p->link->linkB p->link->link=p->link;C p=p->link->link Dp->link=p->link->link
1.4已知x>=y and y>=z 为真,那么x>z or y=z 值为
A真B假C无法确定Dx y z同为正数时为真
1.5某请求被随即分配到四台机器进行处理,分配到每台机器的概率A15% B20% C 30% D 35%, 处理请求的失败概率分别为5% ,4%, 3% 2%,现在请求失败,问由C造成的概率最接近A26% B28% C 30% D 32%
1.6假设我们用d=(a1,a2,….a5)表示无向无环图G的5个顶点的度数,下面给出的哪组值是可能的
A{3,4,4,3,1}B{4,2,2,1,1}C{3,3,3,2,2}D{3,4,3,2,1}
1.7设栈S和队列Q的初始状态为空,元素e1,e2,e3,e4,e5,e6一次压入栈S,一个元素出栈后即进入队列Q,若出队列的顺序为e2,e4,e3,e6,e5,e1则栈S的容量要求最小值为
A2B3C4D5
1.8 在堆排序算法中我们用一个数组A来模拟二叉树T,如果该A[0]存放的是T的根节点,那么AK的父亲节点是
A (K-1)/2 B K/2 C(K+1)/2 D 都不对 ( via: unus.cn )
1.9 现有如下任务需要安排在若干机器上并行完成,每个任务都有开始时间和结束时间(开始和结束时间都包括在任务执行时间内)的要求
任务名称 开始时间 结束时间
a 1 7
b 8 9
c 2 5
d 7 11
e 3 6
f 7 9
g 10 13
则最少需要使用的机器数目为
A1B2C3D4
1.10 在设计一个操作系统时,哪项不是必须考虑的
A 设备管理模块B文件系统模块C用户管理模块D进程管理模块
2.1正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,现给定a和b,需要计算出Q中的前几项,例如,当a=3,b=5,N=6时,序列为3,5,6,9,10,12
(1)设计一个函数void generate(int a,int b,int N ,int * Q)计算Q的前几项
(2)设计测试数据来验证函数程序在各种输入下的正确性
2.2 有一个由大小写组成的字符串,现在需要对他进行修改,将其中的所有小写字母排在答谢字母的前面(大写或小写字母之间不要求保持原来次序),如有可能尽量选择时间和空间效率高的算法 c语言函数原型void proc(char *str) 也可以采用你自己熟悉的语言
2.3 已知一颗无向无环连通图T的所有顶点和边的信息,现需要将其转换为一棵树,要求树的深度最小,请设计一个算法找到所有满足要求的树的根结点,并分析时空复杂度(描述算法即可,无需代码)
1.1关于IP协议那个正确
A IP是TCP上层协议B IP协议是应用层协议C由于两个属于同一层协议,他们之间可以直接通信DIP协议不提供可靠的通信
1.2 关于内存正确的是
A内存的存取速度不能低于cpu速度,否则会造成数据丢失
B程序只有在数据和代码等被调入内存后才能运行
C采用虚拟内存技术后程序可以在硬盘上直接运行
D某计算机的内存容量为16MB,那么他的地址总线为24位
1.3单链表中结点的结构为(data,link),若想删除结点p(不是头节点或者尾结点)的直接后继,则应执行下列哪个操作
A p=p->link ; p->link=p->link->linkB p->link->link=p->link;C p=p->link->link Dp->link=p->link->link
1.4已知x>=y and y>=z 为真,那么x>z or y=z 值为
A真B假C无法确定Dx y z同为正数时为真
1.5某请求被随即分配到四台机器进行处理,分配到每台机器的概率A15% B20% C 30% D 35%, 处理请求的失败概率分别为5% ,4%, 3% 2%,现在请求失败,问由C造成的概率最接近A26% B28% C 30% D 32%
1.6假设我们用d=(a1,a2,….a5)表示无向无环图G的5个顶点的度数,下面给出的哪组值是可能的
A{3,4,4,3,1}B{4,2,2,1,1}C{3,3,3,2,2}D{3,4,3,2,1}
1.7设栈S和队列Q的初始状态为空,元素e1,e2,e3,e4,e5,e6一次压入栈S,一个元素出栈后即进入队列Q,若出队列的顺序为e2,e4,e3,e6,e5,e1则栈S的容量要求最小值为
A2B3C4D5
1.8 在堆排序算法中我们用一个数组A来模拟二叉树T,如果该A[0]存放的是T的根节点,那么AK的父亲节点是
A (K-1)/2 B K/2 C(K+1)/2 D 都不对 ( via: unus.cn )
1.9 现有如下任务需要安排在若干机器上并行完成,每个任务都有开始时间和结束时间(开始和结束时间都包括在任务执行时间内)的要求
任务名称 开始时间 结束时间
a 1 7
b 8 9
c 2 5
d 7 11
e 3 6
f 7 9
g 10 13
则最少需要使用的机器数目为
A1B2C3D4
1.10 在设计一个操作系统时,哪项不是必须考虑的
A 设备管理模块B文件系统模块C用户管理模块D进程管理模块
2.1正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,现给定a和b,需要计算出Q中的前几项,例如,当a=3,b=5,N=6时,序列为3,5,6,9,10,12
(1)设计一个函数void generate(int a,int b,int N ,int * Q)计算Q的前几项
(2)设计测试数据来验证函数程序在各种输入下的正确性
2.2 有一个由大小写组成的字符串,现在需要对他进行修改,将其中的所有小写字母排在答谢字母的前面(大写或小写字母之间不要求保持原来次序),如有可能尽量选择时间和空间效率高的算法 c语言函数原型void proc(char *str) 也可以采用你自己熟悉的语言
2.3 已知一颗无向无环连通图T的所有顶点和边的信息,现需要将其转换为一棵树,要求树的深度最小,请设计一个算法找到所有满足要求的树的根结点,并分析时空复杂度(描述算法即可,无需代码)

intel的笔试题

智力题

1.每天中午从法国塞纳河畔的勒阿佛有一艘轮船驶往美国纽约,在同一时刻纽约也有一艘轮船驶往勒阿佛。已知横渡一次的时间是7天7夜,轮船匀速航行,在同一航线,轮船近距离可见。
请问今天中午从勒阿佛开出的船会遇到几艘从纽约来的船?

2.巴拿赫病故于1945年8月31日。他的出生年份恰好是他在世时某年年龄的平方,问:他是哪年出生的?

答案:

设他在世时某年年龄为x,则x的平方<1945,且x为自然数。其出生年份x的平方-x=x(x-1),他在世年龄1945-x(x-1)。1945的平方根=44.1,则x应为44或略小于此的数。而x=44时,x(x-1)=44×43=1892,算得其在世年龄为1945-1892=53;又x=43时,x(x-1)=43×42=1806,得其在世年龄为1945-1806=139;若x再取小,其在世年龄越大,显然不妥。故x=44,即他出生于1892年,终年53岁。

笔试题目

1.设计一个重采样系统,说明如何anti-alias。

2.y1(n)=x(2n),y2(n)=x(n/2),问:

如果y1为周期函数,那么x是否为周期函数?

如果x为周期函数,那么y1是否为周期函数?

如果y2为周期函数,那么x是否为周期函数?

如果x为周期函数,那么y2是否为周期函数?

3.如果模拟信号的带宽为5kHz,要用8k的采样率,怎么办。

4.某个程序在一个嵌入式系统(200M的CPU,50M的SDRAM)中已经最优化了,换到另一个系统(300M的CPU,50M的SDRAM)中运行,还需要优化吗?
5.x^4+ax^3+x^2+cx+d最少需要做几次乘法。

6.三个float:a,b,c

问值:

(a+b)+c==(b+a)+c

(a+b)+c==(a+c)+b

7.把一个链表反向填空。

8.下面哪种排序法对12354最快?

A. quick sort

B. buble sort

C. merge sort

9.哪种结构平均来讲获取一个值最快?

A. binary tree
B. hash table
C. stack
10.

#include
“stdafx.h”
#include <iostream.h>
struct bit
{ int a:3;
int b:2;
int c:3;
};
int main(int argc, char* argv[])
{
bit s;
char c = (char)&s;
*c = 0x99;
cout <<
s.a <<endl <<s.b<<endl<<s.c<<endl;
return 0;
}

Output:?
11.

挑bug,在linux下运行:
#include <stdio.h>
char
reverse(char str)
{
int len=0, i=0;
char pstr=str, ptemp,pd;
while(
++pstr)
len++;
pstr–;
//ptemp=(char
)malloc(len+1);
ptemp=(char
)malloc(len+1);
pd=ptemp;
while(len–){
*ptemp=*pstr;
ptemp++;
pstr–;
i++;
}
*ptemp=*pstr;
ptemp++;
*ptemp=‘\0’;
return pd;
}
main()
{
char string[40]= “Hello World!”;
char *pstr=string;
printf(“%s”, pstr);
printf(“%s”, reverse(pstr));
}

实验室笔试题

1.写出下列信号的奈亏斯特频率

(1)f(t)=1+cos(2000pait)+sin(4000pait)
(2)f(t)=sin(4000pait)/pait
(3)f(t)=(sin(4000pait)的平方)/pait

2.有两个线程

void producer()
{
while(1)
{
GeneratePacket();
PutPacketIntoBuffer();
Signal(customer);
}
}
void customer()
{
while(1)
{
WaitForSignal();
if(PacketInBuffer>10)
{
ReadAllPackets();
ProcessPackets();
}
}
}
(1)有没有其他方法可以提高程序的性能

(2)可不可以不使用信号之类的机制来实现上述的功能

3.优化下面的程序

(0)sum=0
(1)I=1
(2)T1=4I
(3)T2=address(A)-4
(4)T3=T2[T1]
(5)T4=address(B)-4
(6)T5=4
I
(7)T6=T4[T5]
(8)T7=T3*T5
(9)sum=sum+T6
(10)I=I+1
(11)IF I<20 GOTO (2)

101道微软IT笔试题

Algorithms and Programming

  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 ?
  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.
  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 ].
  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 ].
  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 ].
  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.]
  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.
  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.
  9. Give a very good method to count the number of ones in a “n” (e.g. 32) bit number.
    ANS. Given below are simple solutions, find a solution that does it in log (n) steps.
    Iterative
    function iterativecount (unsigned int n)
    begin
    int count=0;
    while (n)
    begin
    count += n & 0×1 ;
    n >>= 1;
    end
    return count;
    end
    Sparse Count
    function sparsecount (unsigned int n)
    begin
    int count=0;
    while (n)
    begin
    count++;
    n &= (n-1);
    end
    return count ;
    end
  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.
  11. Reverse a linked list.
  12. Insert in a sorted list
  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.
    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.
  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.
  15. Give a fast way to multiply a number by 7.
  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).
  17. Linked list manipulation.
  18. Tradeoff between time spent in testing a product and getting into the market first.
  19. What to test for given that there isn’t enough time to test everything you want to.
  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′.
    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).
  21. Delete an element from a doubly linked list.
  22. Write a function to find the depth of a binary tree.
  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.
  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.
  25. Reverse a linked list.
    Ans: Possible answers -
    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
  26. Write a small lexical analyzer - interviewer gave tokens. expressions like “a*b” etc.
  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)
  28. Write a routine that prints out a 2-D array in spiral order!
  29. How is the readers-writers problem solved? - using semaphores/ada .. etc.
  30. Ways of optimizing symbol table storage in compilers.
  31. A walk-through through the symbol table functions, lookup() implementation etc. - The interviewer was on the Microsoft C team.
  32. A version of the “There are three persons X Y Z, one of which always lies”.. etc..
  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.
  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.
  35. The if (x == 0) y = 0 etc..
  36. Some more bitwise optimization at assembly level
  37. Some general questions on Lex, Yacc etc.
  38. Given an array t[100] which contains numbers between 1..99. Return the duplicated value. Try both O(n) and O(n-square).
  39. Given an array of characters. How would you reverse it. ? How would you reverse it without using indexing in the array.
  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)
  41. Fundamentals of RPC.
  42. Given a linked list which is sorted. How will u insert in sorted way.
  43. Given a linked list How will you reverse it.
  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.
  45. Do a breadth first traversal of a tree.
  46. Write code for reversing a linked list.
  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).
  48. Given an array of integers, find the contiguous sub-array with the largest sum.
    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.
  49. Given an array of length N containing integers between 1 and N, determine if it contains any duplicates.
    ANS. [Is there an O(n) time solution that uses only O(1) extra space and does not destroy the original array?]
  50. Sort an array of size n containing integers between 1 and K, given a temporary scratch integer array of size K.
    ANS. Compute cumulative counts of integers in the auxiliary array. Now scan the original array, rotating cycles! [Can someone word this more nicely?]
    * 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?
    ANS. Can be done in O(k) time i.e. without initializing the auxiliary array!
  51. 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?
    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!
  52. An array of characters. Reverse the order of words in it.
    ANS. Write a routine to reverse a character array. Now call it for the given array and for each word in it.
    * 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?
    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.
  53. An array of pointers to (very long) strings. Find pointers to the (lexicographically) smallest and largest strings.
    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.
  54. Write a program to remove duplicates from a sorted array.
    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;
    }
  55. 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).
  56. 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).
  57. Given 3 lines of assembly code : find it is doing. IT was to find absolute value.
  58. If you are on a boat and you throw out a suitcase, Will the level of water increase.
  59. Print an integer using only putchar. Try doing it without using extra storage.
  60. Write C code for (a) deleting an element from a linked list (b) traversing a linked list
  61. What are various problems unique to distributed databases
  62. Declare a void pointer ANS. void *ptr;
  63. 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
  64. What is a far pointer (in DOS)
  65. What is a balanced tree
  66. 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 A
    |
    |
    O B
    |
    |
    O C
    How do you convert the above linked list to the form without disturbing the property. Write C code for that.
    O P
    |
    |
    O B
    /
    /
    /
    O ? O ?
    determine where do A and C go
  67. Describe the file system layout in the UNIX OS
    ANS. describe boot block, super block, inodes and data layout
  68. In UNIX, are the files allocated contiguous blocks of data
    ANS. no, they might be fragmented
    How is the fragmented data kept track of
    ANS. Describe the direct blocks and indirect blocks in UNIX file system
  69. 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’]);
    }
  70. what is disk interleaving
  71. why is disk interleaving adopted
  72. 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
  73. 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)
  74. 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.
  75. 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.
  76. int *a;
    char *c;
    *(a) = 20;
    *c = *a;
    printf(”%c”,*c);
    what is the output?
  77. Write a program to find whether a given m/c is big-endian or little-endian!
  78. What is a volatile variable?
  79. What is the scope of a static function in C ?
  80. What is the difference between “malloc” and “calloc”?
  81. struct n { int data; struct n* next}node;
    node *c,*t;
    c->data = 10;
    t->next = null;
    *c = *t;
    what is the effect of the last statement?
  82. If you’re familiar with the ? operator x ? y : z
    you want to implement that in a function: int cond(int x, int y, int z); using only ~, !, ^, &, +, |, <<, >> no if statements, or loops or anything else, just those operators, and the function should correctly return y or z based on the value of x. You may use constants, but only 8 bit constants. You can cast all you want. You’re not supposed to use extra variables, but in the end, it won’t really matter, using vars just makes things cleaner. You should be able to reduce your solution to a single line in the end though that requires no extra vars.
  83. You have an abstract computer, so just forget everything you know about computers, this one only does what I’m about to tell you it does. You can use as many variables as you need, there are no negative numbers, all numbers are integers. You do not know the size of the integers, they could be infinitely large, so you can’t count on truncating at any point. There are NO comparisons allowed, no if statements or anything like that. There are only four operations you can do on a variable.
  1. You can set a variable to 0.
  2. You can set a variable = another variable.
  3. You can increment a variable (only by 1), and it’s a post increment.
  4. You can loop. So, if you were to say loop(v1) and v1 = 10, your loop would execute 10 times, but the value in v1 wouldn’t change so the first line in the loop can change value of v1 without changing the number of times you loop.
    You need to do 3 things.
  5. Write a function that decrements by 1.
  6. Write a function that subtracts one variable from another.
  7. Write a function that divides one variable by another.
  8. See if you can implement all 3 using at most 4 variables. Meaning, you’re not making function calls now, you’re making macros. And at most you can have 4 variables. The restriction really only applies to divide, the other 2 are easy to do with 4 vars or less. Division on the other hand is dependent on the other 2 functions, so, if subtract requires 3 variables, then divide only has 1 variable left unchanged after a call to subtract. Basically, just make your function calls to decrement and subtract so you pass your vars in by reference, and you can’t declare any new variables in a function, what you pass in is all it gets.
    Linked lists
    * 86. Under what circumstances can one delete an element from a singly linked list in constant time?
    ANS. If the list is circular and there are no references to the nodes in the list from anywhere else! Just copy the contents of the next node and delete the next node. If the list is not circular, we can delete any but the last node using this idea. In that case, mark the last node as dummy!
    * 87. Given a singly linked list, determine whether it contains a loop or not.
    ANS. (a) Start reversing the list. If you reach the head, gotcha! there is a loop!
    But this changes the list. So, reverse the list again.
    (b) Maintain two pointers, initially pointing to the head. Advance one of them one node at a time. And the other one, two nodes at a time. If the latter overtakes the former at any time, there is a loop!
    p1 = p2 = head;
    do {
    p1 = p1->next;
    p2 = p2->next->next;
    } while (p1 != p2);
  1. Given a singly linked list, print out its contents in reverse order. Can you do it without using any extra space?
    ANS. Start reversing the list. Do this again, printing the contents.
  2. Given a binary tree with nodes, print out the values in pre-order/in-order/post-order without using any extra space.
  3. Reverse a singly linked list recursively. The function prototype is node * reverse (node *) ;
    ANS.
    node * reverse (node * n)
    {
    node * m ;
    if (! (n && n -> next))
    return n ;
    m = reverse (n -> next) ;
    n -> next -> next = n ;
    n -> next = NULL ;
    return m ;
    }
  4. Given a singly linked list, find the middle of the list.
    HINT. Use the single and double pointer jumping. Maintain two pointers, initially pointing to the head. Advance one of them one node at a time. And the other one, two nodes at a time. When the double reaches the end, the single is in the middle. This is not asymptotically faster but seems to take less steps than going through the list twice.
    Bit-manipulation
  5. Reverse the bits of an unsigned integer.
    ANS.
    #define reverse(x)
    (x=x>>16|(0×0000ffff&x)<<16,
    x=(0xff00ff00&x)>>8|(0×00ff00ff&x)<<8,
    x=(0xf0f0f0f0&x)>>4|(0×0f0f0f0f&x)<<4,
    x=(0xcccccccc&x)>>2|(0×33333333&x)<<2,
    x=(0xaaaaaaaa&x)>>1|(0×55555555&x)<<1)
    * 93. Compute the number of ones in an unsigned integer.
    ANS.
    #define count_ones(x)
    (x=(0xaaaaaaaa&x)>>1+(0×55555555&x),
    x=(0xcccccccc&x)>>2+(0×33333333&x),
    x=(0xf0f0f0f0&x)>>4+(0×0f0f0f0f&x),
    x=(0xff00ff00&x)>>8+(0×00ff00ff&x),
    x=x>>16+(0×0000ffff&x))
  6. Compute the discrete log of an unsigned integer.
    ANS.
    #define discrete_log(h)
    (h=(h>>1)|(h>>2),
    h|=(h>>2),
    h|=(h>>4),
    h|=(h>>8),
    h|=(h>>16),
    h=(0xaaaaaaaa&h)>>1+(0×55555555&h),
    h=(0xcccccccc&h)>>2+(0×33333333&h),
    h=(0xf0f0f0f0&h)>>4+(0×0f0f0f0f&h),
    h=(0xff00ff00&h)>>8+(0×00ff00ff&h),
    h=(h>>16)+(0×0000ffff&h))
    If I understand it right, log2(2) =1, log2(3)=1, log2(4)=2….. But this macro does not work out log2(0) which does not exist! How do you think it should be handled?
    * 95. How do we test most simply if an unsigned integer is a power of two?
    ANS. #define power_of_two(x) \ ((x)&&(~(x&(x-1))))
  7. Set the highest significant bit of an unsigned integer to zero.
    ANS. (from Denis Zabavchik) Set the highest significant bit of an unsigned integer to zero
    #define zero_most_significant(h)
    (h&=(h>>1)|(h>>2),
    h|=(h>>2),
    h|=(h>>4),
    h|=(h>>8),
    h|=(h>>16))
  8. Let f(k) = y where k is the y-th number in the increasing sequence of non-negative integers with the same number of ones in its binary representation as y, e.g. f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 1, f(4) = 3, f(5) = 2, f(6) = 3 and so on. Given k >= 0, compute f(k).
    Others
  9. A character set has 1 and 2 byte characters. One byte characters have 0 as the first bit. You just keep accumulating the characters in a buffer. Suppose at some point the user types a backspace, how can you remove the character efficiently. (Note: You cant store the last character typed because the user can type in arbitrarily many backspaces)
  10. What is the simples way to check if the sum of two unsigned integers has resulted in an overflow.
  11. How do you represent an n-ary tree? Write a program to print the nodes of such a tree in breadth first order.
  12. Write the ‘tr’ program of UNIX. Invoked as
    tr -str1 -str2. It reads stdin and prints it out to stdout, replacing every occurance of str1 with str2.
    e.g. tr -abc -xyz
    to be and not to be <- input
    to ye xnd not to ye <- output

Pseudo-random

古老的LCG(linear congruential generator)代表了最好的伪随机数产生器算法。主要原因是容易理解,容易实现,而且速度快。

如果需要高质量的伪随机数,内存充足(约2kb),Mersenne twister算法是个不错的选择。Mersenne twister产生随机数的质量几乎超过任何LCG。不过一般Mersenne twister的实现使用LCG产生种子。
Mersenne twister是Makoto Matsumoto (松本)和Takuji Nishimura (西村)于1997年开发的伪随机数产生器,基于有限二进制字段上的矩阵线性再生。可以快速产生高质量的伪随机数,修正了古老随机数产生算法的很多缺陷。 Mersenne twister这个名字来自周期长度通常取Mersenne质数这样一个事实。常见的有两个变种Mersenne Twister MT19937和Mersenne Twister MT19937-64。
Mersenne Twister有很多长处,例如:周期2^19937 - 1对于一般的应用来说,足够大了,序列关联比较小,能通过很多随机性测试。
用Mersenne twister算法实现的伪随机数版本非常多。例如boost库中的高质量快速随机数产生器就是用Mersenne twister算法原理编写的。

matlab的随机函数产生的随机数都是根据伪随机数序列获取的,在v7.7以上的版本中,有如下的伪随机数产生器:
Mersenne twister,
Multiplicative congruential generator,
Multiplicative lagged Fibonacci generator,
Combined multiple recursive generator,
Shift-register generator summed with linear congruential generator,
Modified subtract with borrow generator。

在Windows下如果用mt_rand()函数替代rand()函数的话效果也会好很多。这是由于mt_rand()用了Mersenne Twister(马其塞旋转)算法来产生随机数。PHP的文档还说:mt_rand() 可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。

Joel Spolsky的七个建议

  • 毕业前练好写作 表达能力->影响力
  • 毕业前学好C语言
    • C语言仍然是当前程序员的共同语言
    • 你能解释为什么while (*s++ = *t++);这句代码的作用是复制字符串吗?
  • 毕业前学好微观经济学 商业领域所有重要理论的基础
  • 你一定要去学微观经济学,因为你必须搞懂供给和需求,你必须明白竞争优势,你必须理解什么是净现值(NPV),什么是贴现,什么是边际效用。只有这样,你才会懂得为什么生意是现在这种做法。
    • 从经营一家公司的角度来看,比起那些不懂的程序员,一个理解基本商业规则的程序员将会更有价值。
    • 别忘了,在编程工作中也有很枯燥的东西。每一项工作都有枯燥难耐的时刻。我不想雇用那些只想干有趣事情的人。
  • 选修有大量编程实践的课程
    • 计算机科学与软件开发不是一回事。
  • 别担心所有工作都被印度人抢走
  • 找一份好的暑期实习工作

Java点点

Java语法

  • final域
    final域一旦赋值后将永远不变,因此非常适合用来定义常量。值得注意的是:如果final域是一个基本类型,那么表明改域的值是不能改变的。若final域是一个对象的引用,则仅仅表明改引用不能被改变,也就是永远指向同一个对象,但是被引用的对象自身却是可以改变的。这跟c++中的常量指针很像(不是指向常量的指针)。初始化final域有着特殊的要求:非静态final域要求在构造函数执行后必须被明确赋值,而静态final域在类初始化完成后必须要被明确赋值。

  • JAR地狱(JAR hell):JAR文件不同版本或路径带来的问题,通常是由于不懂类加载模型导致的。指类路径里JAR包太多这个问题。另外一个“JAR地狱”的解释是“反模式”中的一个概念。DLL地狱(DLL hell):不同版本带来的问题,DLL可见性和多版本问题,在微软的Windows上尤为突出

  • ThreadLocal
    A ThreadLocal can be used to avoid the creation of a new SimpleDateFormat for each call.
    It is needed in a multithread context since the SimpleDateFormat is not thread safe

Spring

Jackson Annotations

  • @JsonIgnoreProperties one of the most common annotations in Jackson – is used to mark a property or a list of properties to be ignored at the class level.如果是代理类,由于无法标记在属性或方法上,所以,可以标记在类声明上;也作用于反序列化时的字段解析。作用在类上,用来说明有些属性在序列化/反序列化时需要忽略掉,可以将它看做是@JsonIgnore的批量操作,但它的功能比@JsonIgnore要强,比如一个类是代理类,我们无法将将@JsonIgnore标记在属性或方法上,此时便可用。标注在类声明上,它还有一个重要的功能是作用在反序列化时解析字段时过滤一些未知的属性,否则通常情况下解析到我们定义的类不认识的属性便会抛出异常。
  • @JsonIgnore The @JsonIgnore annotation is used to mark a property to be ignored at the field level.一般标记在属性或方法上;作用于序列化与反序列化。作用在字段或方法上,用来完全忽略被注解的字段和方法对应的属性,即便这个字段或方法可以被自动检测到或者还有其他的注解
  • @JsonPropertyOrder,注释在类声明中
  • @JsonAutoDetect 看上面自动检测,不再重复
  • @JsonProperty 作用在字段或方法上,用来对属性的序列化/反序列化,可以用来避免遗漏属性,同时提供对属性名称重命名,比如在很多场景下Java对象的属性是按照规范的驼峰书写,但是实际展示的却是类似C-style或C++/Microsolft style
  • @JsonUnwrapped 作用在属性字段或方法上,用来将子JSON对象的属性添加到封闭的JSON对象
  • @JsonIdentityInfo 2.0+版本新注解,作用于类或属性上,被用来在序列化/反序列化时为该对象或字段添加一个对象识别码,通常是用来解决循环嵌套的问题,比如数据库中的多对多关系,通过配置属性generator来确定识别码生成的方式,有简单的,配置属性property来确定识别码的名称,识别码名称没有限制。
  • @JsonNaming jackson 2.1+版本的注解,作用于类或方法,注意这个注解是在jackson-databind包中而不是在jackson-annotations包里,它可以让你定制属性命名策略,作用和前面提到的@JsonProperty的重命名属性名称相同。

多态类型处理

jackson允许配置多态类型处理,当进行反序列话时,JSON数据匹配的对象可能有多个子类型,为了正确的读取对象的类型,我们需要添加一些类型信息。可以通过下面几个注解来实现:

  • @JsonTypeInfo 作用于类/接口,被用来开启多态类型处理,对基类/接口和子类/实现类都有效

工具

9 tools to help you with Java Performance Tuning
http://blog.idrsolutions.com/2014/06/java-performance-tuning-tools/

JWT

JWT是 Json Web Token 的缩写。它是基于 RFC 7519 标准定义的一种可以安全传输的 小巧 和 自包含 的JSON对象。由于数据是使用数字签名的,所以是可信任的和安全的。JWT可以使用HMAC算法对secret进行加密或者使用RSA的公钥私钥对来进行签名。

JWT是由三段组成的,按官方的叫法分别是header(头)、payload(负载)和signature(签名):

header.payload.signature

一个比较成熟的JWT类库,叫 jjwt ( https://github.com/jwtk/jjwt )。这个类库可以用于Java和Android的JWT token的生成和验证。

Java JWT: JSON Web Token for Java and Android
https://github.com/jwtk/jjwt

JWTs are incredibly cool for authentication because they let us implement reliable Single Sign-On (SSO) with low overhead on any platform (native, web, VR, whatever…) and across domains. JWTs are a strong alternative to pure cookie or session based auth with simple tokens or SAML, which can fail miserably in native app implementations. We can even use cookies with JWTs if we really want.

For our purposes, we just need to know how to use JWTs within our authentication workflow. When a user logs into our app, the server will check their email and password against the database. If the user exists, we’ll take their {email: , password: } combination, turn it into a lovely JWT, and send it back to the client. The client can store the JWT forever or until we set it to expire.

Whenever the client wants to ask the server for data, it’ll pass the JWT in the request’s Authorization Header (Authorization: Bearer ). The server will decode the Authorization Header before executing every request, and the decoded JWT should contain {email: , password: }. With that data, the server can retrieve the user again via the database or a cache to determine whether the user is allowed to execute the request.

The Expired Password Problem
We still have one last thing that needs modifying in our authorization setup. When a user changes their password, we issue a new JWT, but the old JWT will still pass verification! This can become a serious problem if a hacker gets ahold of a user’s password. To close the loop on this issue, we can make a clever little adjustment to our UserModel database model to include a version parameter, which will be a counter that increments with each new password for the user. We’ll incorporate version into our JWT so only the newest JWT will pass our security.

IBM developerWorks

算法

随机数生成器

使您的软件运行起来
摆弄数字
真正安全的软件需要精确的随机数生成器
https://www.ibm.com/developerworks/cn/security/playing/index.html

java

Java 编程的动态性,第 1 部分
类和类装入
研究类以及 JVM 装入类时所发生的情况
https://www.ibm.com/developerworks/cn/java/j-dyn0429/

class类文件
cafe babe 任何 Java 二进制类(甚至是文件系统中没有出现的类)都需要以这四个字节作为开始
0000 次版本 0
002e 主版本 46
001a 常量池中项的总数
后面是实际的常量池数据 常量池往往占到二进制类大小的一半或更多,但平均下来可能要少一些。

方法的可执行代码 用 JVM 的指令形式表示该代码,一般称为 字节码
构成类文件可执行部分的字节码实际上是针对特定类型的计算机 ― JVM ― 的机器码
JVM被称为 虚拟机,因为它被设计成用软件来实现,而不是用硬件来实现。每个用于运行 Java 平台应用程序的 JVM 都是围绕该机器的实现而被构建的。
JVM使用堆栈体系结构,这意味着在使用指令操作数之前要先将它们装入内部堆栈。
早期的(第一代)JVM 基本上是虚拟机字节码的解释器。这些虚拟机实际上 的确相对简单,但存在严重的性能问题 ― 解释代码的时间总是会比执行本机代码的时间长。
第二代 JVM 添加了 即时(just-in-time,JIT)转换。在第一次执行 Java 字节码之前,JIT 技术将它编译成本机代码,从而对于重复执行提供了更好的性能。
当代 JVM 的性能甚至还要好得多,因为使用了适应性技术来监控程序的执行并有选择地优化频繁使用的代码。

JAR 只是类文件的容器

增加环境变量 JAVA_OPTS -verbose 可以查看类加载过程

C 和 C++ 这些编译成本机代码的语言通常在编译完源代码之后需要链接这个步骤

使用 Java 语言,由编译器生成的类在被装入到 JVM 之前通常保持原状

链接类不是一个独立步骤,它是在 JVM 将这些类装入到内存时所执行作业的一部分

能装入独立的类集合这一灵活性是 Java 平台的一个重要特性。尽管这个特性很有用,但是它在某些情况中会产生混淆。

一个令人混淆的方面是处理 JVM 类路径这样的老问题。

使用多个类装入器还可能引起其它类型的混淆。身份危机(class identity crisis)

JDK5.0以后不需要配classpath了,只要把path配好就行

敏捷开发中高质量 Java 代码开发实践
https://www.ibm.com/developerworks/cn/java/j-lo-agile/

步骤一:统一编码规范、代码样式
步骤二:静态代码分析
步骤三:单元测试
步骤四:持续集成(Continuous Integration)
步骤五:代码评审和重构(Code Review)

使用原汁原味的 Java 语言
非 Java 原生程序员的语言流畅性
https://www.ibm.com/developerworks/cn/java/j-noaccent.html

Java 的惯例中 main()方法的参数名为 args,而不是 argv:public static void main(String[] args)

深入探讨 Java 类加载器
https://www.ibm.com/developerworks/cn/java/j-lo-classloader/

常用 Java 静态代码分析工具的分析与比较
https://www.ibm.com/developerworks/cn/java/j-lo-statictest-tools/

使用 Java 开源工具建立一个灵活的搜索引擎
揭示开源的力量
https://www.ibm.com/developerworks/cn/java/j-lo-sefrmk/

几种任务调度的 Java 实现方法与比较
https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/index.html

用 Quartz 进行作业调度
Quartz API 采用多面方式在 Java 应用程序中进行任务调度
https://www.ibm.com/developerworks/cn/java/j-quartz/

在 Web 项目中应用 Apache Shiro
https://www.ibm.com/developerworks/cn/java/j-lo-shiro/

从虚拟机视角谈 Java 应用性能优化
https://www.ibm.com/developerworks/cn/java/j-lo-jvm-perf/

使用 VisualVM 进行性能分析及调优
https://www.ibm.com/developerworks/cn/java/j-lo-visualvm/

优化 Java 垃圾收集器改进系统性能
https://www.ibm.com/developerworks/cn/java/j-lo-optimize-gc/

Java的内存泄漏
https://www.ibm.com/developerworks/cn/java/l-JavaMemoryLeak/

Java 性能优化之 String 篇
https://www.ibm.com/developerworks/cn/java/j-lo-optmizestring/

Java Web 高性能开发,第 1 部分
前端的高性能
https://www.ibm.com/developerworks/cn/java/j-lo-javawebhiperf1/

您不知道的 5 件事……
JVM 命令行标志
调优 JVM 性能和 Java 运行时
https://www.ibm.com/developerworks/cn/java/j-5things11/index.html

Java.next, Common ground in Groovy, Scala, and Clojure, Part 1
Explore how these next-generation JVM languages handle operator overloading
https://www.ibm.com/developerworks/java/library/j-jn2/index.html

Java.next, Common ground in Groovy, Scala, and Clojure, Part 3
Rethinking exceptions, expressions, and emptiness
https://www.ibm.com/developerworks/java/library/j-jn4/index.html

使用JMeter进行性能测试
https://www.ibm.com/developerworks/cn/java/l-jmeter/

使用 JMeter 完成常用的压力测试
https://www.ibm.com/developerworks/cn/opensource/os-pressiontest/

启用动态 HTTP 压缩
在各种 Web 服务器上通过动态压缩节省带宽
https://www.ibm.com/developerworks/cn/web/wa-httpiis/

最佳实践:更好的设计你的 REST API
https://www.ibm.com/developerworks/cn/web/1103_chenyan_restapi/

NIO 入门
https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.html

Merlin 给 Java 平台带来了非阻塞 I/O
新增的功能大幅降低了线程开销
https://www.ibm.com/developerworks/cn/java/j-javaio/

深入分析 Java 中的中文编码问题
https://www.ibm.com/developerworks/cn/java/j-lo-chinesecoding/

Java SSL/TLS 安全通讯协议介绍
Java 的安全通讯
https://www.ibm.com/developerworks/cn/java/j-lo-ssltls/

Java 安全套接字编程以及 keytool 使用最佳实践
https://www.ibm.com/developerworks/cn/java/j-lo-socketkeytool/

开源规则流引擎实践
https://www.ibm.com/developerworks/cn/opensource/os-drools/index.html

使用 Drools 规则引擎实现业务逻辑

如果你的业务场景中有很多复杂的业务逻辑/业务策略,而这些业务策略又经常发生变化,那么你就可以引入规则引擎技术。

开发 Spring Redis 应用程序
使用 Redis 作为数据存储来构建基于 Spring 的应用程序
https://www.ibm.com/developerworks/cn/java/os-springredis/index.html

Java 编程中的 OAuth 2.0 客户端,第 1 部分
资源所有者密码凭据授权
https://www.ibm.com/developerworks/cn/java/se-oauthjavapt1/index.html

Java 编程中的 OAuth 2.0 客户端,第 2 部分
客户端凭据授权
https://www.ibm.com/developerworks/cn/java/se-oauthjavapt2/index.html

Java 编程中的 OAuth 2.0 客户端,第 3 部分
认证码授权
https://www.ibm.com/developerworks/cn/java/se-oauthjavapt3/index.html

Java 开发 2.0
现实世界中的 Redis
Redis 如何在包含大量读取操作的应用程序中战胜 memcached
https://www.ibm.com/developerworks/cn/java/j-javadev2-22/

全面分析 Spring 的编程式事务管理及声明式事务管理
https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/

Spring 事务管理高级应用难点剖析
第 1 部分
https://www.ibm.com/developerworks/cn/java/j-lo-spring-ts1/index.html

Spring 事务管理高级应用难点剖析
第 2 部分
https://www.ibm.com/developerworks/cn/java/j-lo-spring-ts2/

Spring 事务管理高级应用难点剖析
第 3 部分
https://www.ibm.com/developerworks/cn/java/j-lo-spring-ts3/index.html

使用 Spring 进行单元测试
https://www.ibm.com/developerworks/cn/java/j-lo-springunitest/

使用 Apache JMeter 测试基于云的应用程序
学习使用 JMeter 进行 RESTful API 测试的有效技术和最佳实践
https://www.ibm.com/developerworks/cn/cloud/library/cl-jmeter-restful/

让开发自动化
用 Eclipse 插件提高代码质量
在 Eclipse 中使用 5 个有用的插件来自动化代码质量分析
https://www.ibm.com/developerworks/cn/java/j-ap01117/

常用 Java Profiling 工具的分析与比较
https://www.ibm.com/developerworks/cn/java/j-lo-profiling/

Java Web 高性能开发,第 2 部分
前端的高性能
https://www.ibm.com/developerworks/cn/java/j-lo-javawebhiperf2/

在 Java 应用程序中使用 Elasticsearch
高性能 RESTful 搜索引擎和文档存储快速入门指南
https://www.ibm.com/developerworks/cn/java/j-use-elasticsearch-java-apps/index.html

Java 应用性能调优实践
让 Java 应用运行更快:性能调优工具及实践
https://www.ibm.com/developerworks/cn/java/j-lo-performance-tuning-practice/

通过零拷贝实现有效数据传输
零拷贝,零开销
https://www.ibm.com/developerworks/cn/java/j-zerocopy/

Java 性能分析工具 , 第 1 部分
操作系统工具
https://www.ibm.com/developerworks/cn/java/j-lo-performance-analysissy-tools/

Java 性能测试的四项原则
https://www.ibm.com/developerworks/cn/java/j-lo-java-performance-testing/

Groovy 使 Spring 更出色,第 1 部分: 集成的基础知识

Java 8 Annotation 新特性在软件质量和开发效率方面的提升

使用 Groovy 构建 DSL

Groovy:Java 程序员的 DSL

SQL

SQL 语句性能调优
初级篇 —— 简单查询语句的调优
https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1002limh/index.html

SQL 在常用报表业务中的归并、转换与信息汇总中的应用技巧
https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1008yangxy/

JavaScript

Understand memory leaks in JavaScript applications
Detect and address memory issues
https://www.ibm.com/developerworks/library/wa-jsmemory/index.html

拥抱原型面向对象编程
https://www.ibm.com/developerworks/cn/web/wa-protoop/index.html

浅谈 JavaScript 编程语言的编码规范
https://www.ibm.com/developerworks/cn/web/1008_wangdd_jscodingrule/

使用 Chrome 开发者工具进行 JavaScript 问题定位与调试
https://www.ibm.com/developerworks/cn/web/1410_wangcy_chromejs/index.html

JavaScript 中的类

用 Maven 做项目构建

POM 即 Project Object Module,项目对象模型,在 pom.xml 文件中定义了项目的基本信息、源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的 url、以及构建项目所用的插件,依赖继承关系。

Maven 本质上是一个插件框架,它的核心并不执行任何具体的构建任务,仅仅定义了抽象的生命周期,所有这些任务都交给插件来完成的。每个插件都能完成至少一个任务,每个任务即是一个功能,将这些功能应用在构建过程的不同生命周期中。这样既能保证拿来即用,又能保证 maven 本身的繁杂和冗余。

可以用 去除这种依赖的传递

可以在当前的 POM 文件中使用 元素声明排除依赖,exclusions 可以包含一个或者多个 exclusion 子元素,因此可以排除一个或者多个传递性依赖。

Nexus是Maven仓库管理器,用来搭建一个本地仓库服务器,这样做的好处是便于管理,节省网络资源,速度快,还有一个非常有用的功能就是可以通过项目的SNAPSHOT版本管理,来进行模块间的高效依赖开发

ES6

新 JavaScript 中的变量声明等功能

React

React:创建可维护、高性能的 UI 组件
使用 React JavaScript 库创建可维护的 Web UI,超越浏览器 DOM
https://www.ibm.com/developerworks/cn/web/wa-react-intro/index.html

React 介绍及实践教程
https://www.ibm.com/developerworks/cn/web/1509_dongyue_react/

redux

Redux 简介

状态管理是软件开发的最困难方面之一,这使得状态管理错误成为了几乎所有 bug 的源头。

Redux,这是一个针对 JavaScript 应用程序的可预测的状态容器。
Redux 不仅有助于状态管理,还使得实现一些高级特性变得很简单,比如无限撤销/重做和实时编辑时间旅行 (live-editing time travel)。

尽管 Redux 来自 React 社区,但它并不依赖于 React。

Redux 是 Facebook 的 Flux 架构的一种简化实现。

Flux 在本质上采用了模型-视图-控制器 (MVC) 的结构

Redux 有 3 条原则:

  • 应用程序状态存储在单个对象中。
  • 应用程序状态不可变,只能通过描述状态更改的操作 彻底替换。
  • 缩减程序根据当前状态和某个操作来创建下一个状态。

Redux 简介

结合使用 Redux 和 React

Unix

对话 UNIX,第 4 部分
UNIX 所有权和权限管理
共享信息
https://www.ibm.com/developerworks/cn/aix/library/au-speakingunix4/index.html

UNIX 文件的权限位 表示了三类用户(您本人、您的一个组和其他用户)的某一种特定的权限
10 个字符组成的序列
每个字符都是一个开/关设置或位

起始位表示该文件是否为目录(通常,起始位 表示该文件是否为特殊文件。如果该文件是特殊文件,起始字符 d 表示目录、l 表示符号链接,等等)。这个设置是无法改变的。

接下来的三位(用蓝色表示的)分别表示您 对该文件的读、写和执行权限。您可以禁用写权限位,例如要防止删除文件。(是的,要删除一个文件,您需要写权限。)

接下来的三位(用绿色表示的)表示组 对该文件的读、写和执行权限。

最后的三位(用橙色表示的)表示所有其他 用户(即除了您自己以及您的组中的成员之外的所有用户)的权限。

您可以使用 chmod(更改模式 change mode)命令修改相应的权限(除了目录位之外)。您可以使用 chgrp(更改组 change group)命令来修改文件所属的组。(超级用户 root 也可以使用 chown 或 change owner 命令来更改文件所有权。)

find . -type f -print | sort | uniq

您可以使用符号 ~(波浪符号)引用您的 home 目录。您还可以使用 $HOME 环境变量引用您的 home 目录

许多程序可以从一个名为 .netrc(读做 net-r-c)的文件中读取您的凭据,该文件通常位于 ~/.netrc 目录中。

Linux 新用户的基本任务
Linux 安装好后,下一步该做什么?
https://www.ibm.com/developerworks/cn/linux/tutorials/l-basics/

学习 Linux,101
LPIC-1 路线图
关于 LPIC-1 考试准备的 developerWorks 文章指南
https://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-map/

学习 Linux,101
创建和更改硬链接和符号链接
为同一文件使用多个名称
https://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-104-6/

学习 Linux,101
管理文件权限和所有权
在您的文件上设置正确的安全性
https://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-104-5/index.html

学习 Linux,101
RPM 和 YUM 包管理
添加新软件并及时更新系统
https://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-102-5/

Bash 实例,第一部分
Bourne again shell (bash) 基本编程
https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-1/

Bash 实例,第 2 部分
更多的 bash 基本编程
https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-2/

Bash 实例,第 3 部分
探讨 ebuild 系统
https://www.ibm.com/developerworks/cn/linux/shell/bash/bash-3/

系统管理员工具包
充分利用 bash
https://www.ibm.com/developerworks/cn/aix/library/au-satbash.html

LPI 102 考试准备,主题 109
Shell、脚本、编程和编译
初级管理(LPIC-1)主题 109
https://www.ibm.com/developerworks/cn/education/linux/l-lpic1109/index.html

LPI 102 考试准备,主题 109
Shell、脚本、编程和编译
初级管理(LPIC-1)主题 109
https://www.ibm.com/developerworks/cn/education/linux/l-lpic1109/index.html

Linux 编程和系统管理新手入门
https://www.ibm.com/developerworks/cn/linux/newto/index.html

Windows 到 Linux 之旅: 系列文章概述
指引开发者走向 Linux 的路线图
https://www.ibm.com/developerworks/cn/linux/l-roadmap/index.html

Windows 到 Linux 之旅: 第 4 部分. 用户管理

Windows 到 Linux 之旅: 第 6 部分. 使用分区和文件系统

数据库

超越 MySQL
对流行数据库进行分支
https://www.ibm.com/developerworks/cn/opensource/os-beyondmysql/

LAMP 系统性能调优,第 3 部分
MySQL 服务器调优
利用服务器的几个调优技巧,让 MySQL 服务器飞速运行
https://www.ibm.com/developerworks/cn/linux/l-tune-lamp-3.html

Web

响应式 Web 设计技巧
https://www.ibm.com/developerworks/cn/web/1506_zhangqun_responsiveweb/

利用 Sass 改善 CSS 预处理

Syntactically awesome stylesheets (Sass) 是一种元语言和层叠式样式表 (CSS) 预处理程序

Sass 主要使用 Ruby 来实现

html5

使用 HTML 5 创建移动 Web 应用程序

第 1 部分: 联合使用 HTML 5、地理定位 API 和 Web 服务来创建移动混搭程序

第 2 部分: 使用 HTML 5 开启移动 Web 应用程序的本地存储

localStorage API

第 3 部分: 使用 HTML 5 支持移动 Web 应用程序离线工作

第 4 部分: 使用 Web Workers 来加速您的移动 Web 应用程序

Web Worker 规范

从 Android 2.0 开始,Android 浏览器就拥有了对 HTML 5 Web Worker 规范的全面支持。

第 5 部分: 使用 HTML 5 开发新的可视化 UI 特性

所有基于 Webkit 的浏览器都能实现 Canvas 并极大地优化其性能。

工具类

ImageMagick 魔咒
面向用户和程序员等受众的图像处理
https://www.ibm.com/developerworks/cn/opensource/os-imagemagick/index.html

其它

利用 squid 反向代理提高网站性能
https://www.ibm.com/developerworks/cn/linux/l-cn-squid/

MQ 遥测传输 (MQTT) V3.1 协议规范
https://www.ibm.com/developerworks/cn/webservices/ws-mqtt/index.html

使用 Nginx 提升网站访问速度
https://www.ibm.com/developerworks/cn/web/wa-lo-nginx/

管理移动云套接字连接
云提供商如何管理移动应用程序与云之间的连接
https://www.ibm.com/developerworks/cn/cloud/library/cl-mobilesockconnect/

配置 Tomcat 和 Wireshark 来获取并解码 SSL 通信
调试安全通信
https://www.ibm.com/developerworks/cn/web/tutorials/wa-tomcat/

使用 OpenSSL API 进行安全编程
创建基本的安全连接和非安全连接
https://www.ibm.com/developerworks/cn/linux/l-openssl.html

利用物联网 (IoT)
探索 IoT 的 7 个重要概念和开始使用它的 4 个步骤
https://www.ibm.com/developerworks/cn/iot/iot-key-concepts/index.html

了解 IT 即流程
如何自动化企业中的 IT 操作
https://www.ibm.com/developerworks/cn/websphere/techjournal/1510_brown-trs/1510_brown.html

使用访问令牌保护微服务

量子计算

量子计算入门
棘手问题的简便解法指南
https://www.ibm.com/developerworks/cn/linux/other/quant/

LLVM

使用 LLVM 框架创建一个工作编译器,第 1 部分

使用 LLVM 框架创建有效的编译器,第 2 部分

LLVM 拥有自己的前端:名为 clang 的一种工具(恰如其分)。Clang 是一种功能强大的 C/C++/Objective-C 编译器,其编译速度可以媲美甚至超过 GNU Compiler Collection (GCC) 工具(参见 参考资料 中的链接,获取更多信息)。更重要的是,clang 拥有一个可修改的代码基,可以轻松实现定制扩展。

blockchain

区块链技术基础:商业账本简介

区块链技术基础:术语和用例

Docker

Docker:带给现代开发人员的福利

Docker 就像是用于 DevOps 的一把瑞士军刀,这一点已得到充分证明。但 Docker 托管的应用程序容器的用途不仅是在云中部署服务器。Docker 容器还可以在许多常见的开发场景中帮助开发和显著提高生产力。

quotes

The gap between the best software engineering practice and the average practice is very wide—perhaps wider than in any other engineering discipline. A tool that disseminates good practice would be important. — Fred Brooks

The laws of nature are but the mathematical thoughts of God. — Euclid

“Stop being so negative, Steven!”, “Why do you always assume the worst?”, “You’re such a ‘glass half empty’ person, Steven.”

我能召唤遥远的精灵。
那又怎么样,我也可以,谁都可以,问题是你真的召唤的时候,它们会来吗? - 莎士比亚,《亨利四世》,第一部分

I can call spirits from the vasty deep.
Why, so can I, or so can any man; but will they come when you do call for them? - SHAKESPEARE, KING HENRY IV, Part I

GLENDOWER: I can call spirits from the vasty deep.
HOTSPUR: Why, so can I, or so can any man; But will they come when you do call for them?

William Shakespeare, King Henry IV, Part 1

他过去所做的许诺,是非凡的;
而他今天的执行,则什么也不是。
莎士比亚,《亨利八世》

只能根据过去判断将来。 - 帕特里克·亨利
然而永远无法根据过去规划将来。 - 埃德蒙·伯克
I know no way of judging the future but by the past. - PATRICK HENRY
You can never plan the future by the past. - EDMUND BURKE

普遍的做法是,选择一种方法,试试看;如果失败了,没关系,再试试别的。不管怎么样,重要的是先去尝试。 - 富兰克林 D. 罗斯福
It is common sense to take a method and try it. If it fails, admit it frankly and try another. But above all, try something. - FRANKLIN D. ROOSEVELT

Donald Knuth 曾经指出,“提前优化是万恶之源”。
“premature optimization is the root of all evil,” as Donald Knuth once put it

Cliff Click expressed the same sentiment this way:
Premature optimization is the root of all evil is more true today than ever before.

Hope for the best and prepare for the worst

Fear not the future ,Weep not for the past

Men are judged by what they do

There is no time like the present

We are all slaves of opinion

Practice is the best master

The language of truth is simple

A man must take the consequence of his own deeds

Great hopes makes great men

Life is short and time is swift

Example is better than precept

Practice what you preach

Practice makes perfect

Easier said than done

It is never too late to mend

Look not too high, lest something fall into your eye.

Plain living and high thinking

A good heart conquers ill fortune

A little body often harbors a great soul

期刊摘句

你以为买到的是“知识”,其实只是“知道”。你以为买到的是“掌握”,其实只是囤积了一堆“知道”。 —— 学者方绍伟谈网络时代的“知识囤积症”

囤书如山倒,读书如抽丝,我早已经囤下后半辈子都读不完的书,但出于占有欲,我还是不断在买书。 (摘自《读者》2018年08期)

一个人好像是一个分数,他的实际才能好比分子,而他对自己的估价好比分母,分母越大则分数值愈小。(托尔斯泰《人生论》)

HALT(hungry, angry, lonely, tired)

手中事和身边人。 —— 对一个人的价值最准确的考量标准

人工智能时代对人类最大的威胁是,它让我们将虚拟社群的联系与真实事物混为一谈。 —— 哈佛大学教授桑德尔谈技术对人类社会的改变

盯住领域里最牛的那几个人,他们就是方向。— 杨振宁

Linux常用知识点

命令

最常用

man 帮助(按q退出man)
ls
pwd 输出工作目录
cd
mkdir
find 和 locate
cp
mv 改名、移动
more
kill 使用ps进程ID,kill进程
sudo
passwd 更改密码

sudo su

查看日志:tail -n 100 catalina.out

复制文件:
sudo cp kaopurServer.war /opt/apache-tomcat-8.0.24/webapps/

查看目录
ll

cat /etc/passwd

cat -n song.txt

给sh脚本增加执行权限:chmod +x yourshfile.sh

mkdir kaopurServer

cd kaopurServer

tar -zxvf ../kaopurServer.tar.gz

sudo \cp -rf kaopurServer/* /opt/apache-tomcat-8.0.24/webapps/kaopurServer

rm -f index.jsp

rm -rf WEB-INF/

如此才会覆盖:
sudo \cp -rf kaopurServer/* /opt/apache-tomcat-8.0.24/webapps/kaopurServer

sudo tar -zxf /home/hl/apache-activemq-5.9.1-bin.tar.gz

cat activemq.xml

linux下载文件:
curl -O http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.0.28/bin/apache-tomcat-8.0.28.tar.gz

curl -O -L http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.27.tar.gz

xshell中文乱码:修改Terminal的字符集

$ ps -ef|grep tfs

curl -o emqttd-centos64-0.13.0-beta-20151108.zip -L http://emqtt.io/downloads/centos

cat /etc/redhat-release

linux常用命令
在 Linux 中用的是正斜杠 (/),而不是您所熟悉的反斜杠 ()。反斜杠也用到了,但只是用来说明命令需要换行继续,这样可以提高比较长的命令的可读性。

ls -l 以长格式列出文件,包括文件大小、日期和时间、属性
ls -t 对文件以时间排序
ls -S 对文件以大小排序
ls -r 与一个排序开关组合起来使用,逆序排列。 ls -t 将最新的文件显示在列表的顶部。 ls -t 将最新的文件显示在底部。
ls -h 易读格式。使用 k、M、G 等来标识文件的大小,而不是以字节为单位。
ls -a 显示目录中所有的文件,包括隐藏文件

cp -R 递归地复制文件;当需要复制整个目录时会用到
cp -f 强制复制并覆盖已有的文件,不询问用户
cp -l 链接文件,而不是复制

使用 mv 命令来移动和重命名文件。这个命令的工作方式基本上与 DOS 中的 move 命令一样,不过它可以移动整个目录结构及所有文件。

cat
使用 cat 命令来查看文件的内容。它相当于 DOS 中的 type 命令。它将把文件的内容转储到另一个文件、屏幕或者其他命令。 cat 是concatenate 的简写,还可以将一系列的文件合并为一个大文件。

more
使用命令 more 可以以分页的方式查看文件。它基本上与 DOS 中的 more 命令相同。

less
less 命令也是用来查看文件,但是它支持上下滚屏以及在文档中进行文本搜索。

man
使用 man 命令来查看命令的文档。man 是 manual 的缩写。几乎每一个命令都有相应的文档。要深入了解 man ,请输入以下命令:

man man

info
info 命令与 man 命令类似,不过它提供了超链接文本,可以更方便地浏览文档。

curl -O -L http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.27.tar.gz

md5sum mysql-5.6.27.tar.gz

md5sum apache-tomcat-8.0.28.tar.gz

sudo tar -zxvf ~/apache-tomcat-8.0.28.tar.gz

sudo rm -r apache-tomcat-8.0.28/

/usr/apache-tomcat-8.0.28/webapps>sudo rm -r examples/

/usr>sudo rm -fr emqttd/

/home/hzg>sudo unzip emqttd-centos64-0.13.0-beta-20151108.zip -d /usr/

/usr/emqttd/bin>sudo ./emqttd console

/usr/emqttd/bin>./emqttd_ctl status

/opt/apache-tomcat-8.0.28/bin>ps -ef | grep tomcat

停止tomcat:
kill -9 2250

查看tomcat日志
/opt/apache-tomcat-8.0.28/logs>tail -n 50 catalina.out

curl -O -L http://download.redis.io/releases/redis-3.0.5.tar.gz

/home/hzg>sudo tar zxf redis-3.0.5.tar.gz -C /opt/

redis的java client
Jedis
lettuce
Redisson

/home/hzg>netstat -tnl | grep 3306

[root@instance-dqg4v0wr-3 ~]# du -sh
36K .

more /etc/passwd

lscpu

free -m

使用 su 或者 sudo -s 命令变为另一个用户
可以使用 whoami 命令来检查您当前有效的 id
可以使用 groups 命令找出您在什么组中

Linux 中的所有用户都存放于 /etc/passwd 文件中。您可以使用 more 命令来分页查看这个文件:
more /etc/passwd

who
功能:列出当前系统注册的用户
$who am i 列出当前系统使用者身份

ps
功能:显示后台进程的有关信息。单独使用ps将显示由
当前终端启动的后台进程的PID、终端号、进程已执行时间以及启动该进程的命令
语法:ps -options
参数说明:
-e:显示系统内当前运行的所有后台进程
-f:除ps所提供的信息外,还显示用户名、PPID(父进程ID)及启动时间
-l:除ps所提供的信息外,还显示 User ID, PPID 和进程优先级

kill
功能:杀死后台进程
语法:kill -num PID

find
功能:在一个或多个目录中查找符合指定条件的文件,
显示文件名或对这些文件进行特定的操作
语法:find path expression

find . –name ‘unix’ –print
find . –name ‘m*’ –exec ls –l {} ;
find . -perm 644 -mtime +4 –print

grep
功能:在文件或标准输入中搜索与指定格式相匹配的行
语法:grep [options] pattern [file1 file 2..]
grep –v mail .profile
grep “^a[0-9]z$” filename
grep a websm.log

vi

:w :保存当前文件
:x:保存当前文件并退出
:q:退出vi
:q! 放弃所做修改直接退出到shell
:q 不退出
ZZ / :x 如果需要保存则保存,之后退出到shell
:w newfile / :w!
:wq 先保存文件,再退出到shell
回车后进行命令模式
在命令模式按a(光标之后)/ A(光标所在行的最后) / i(光标处)/I(光标所在行的开头) / o / O ,进入文本输入模式
在文本输入模式下,按Esc,回到命令模式
末行模式,Ex转义模式: 在命令模式下按:进行末行模式,末行命令执行后自动回到命令模式

插入文本类命令

i :在光标前
I :在当前行首
a:光标后
r:替换当前字符

删除命令

x或X:删除一个字符,x删除光标后的,而X删除光标前的
ndd:删除当前行及其后n-1行
dd 删除当前行

搜索及替换命令

/pattern:从光标开始处向文件尾搜索pattern
?pattern:从光标开始处向文件首搜索pattern
:g/p1/s//p2/g:将文件中所有p1均用p2替换

P vs NP Problem

If it is easy to check that a solution to a problem is correct, is it also easy to solve the problem? This is the essence of the P vs NP question. Typical of the NP problems is that of the Hamiltonian Path Problem: given N cities to visit (by car), how can one do this without visiting a city twice? If you give me a solution, I can easily check that it is correct. But I cannot so easily (given the methods I know) find a solution.

Suppose that you are organizing housing accommodations for a group of four hundred university students. Space is limited and only one hundred of the students will receive places in the dormitory. To complicate matters, the Dean has provided you with a list of pairs of incompatible students, and requested that no pair from this list appear in your final choice. This is an example of what computer scientists call an NP-problem, since it is easy to check if a given choice of one hundred students proposed by a coworker is satisfactory (i.e., no pair taken from your coworker’s list also appears on the list from the Dean’s office), however the task of generating such a list from scratch seems to be so hard as to be completely impractical. Indeed, the total number of ways of choosing one hundred students from the four hundred applicants is greater than the number of atoms in the known universe! Thus no future civilization could ever hope to build a supercomputer capable of solving the problem by brute force; that is, by checking every possible combination of 100 students. However, this apparent difficulty may only reflect the lack of ingenuity of your programmer. In fact, one of the outstanding problems in computer science is determining whether questions exist whose answer can be quickly checked, but which require an impossibly long time to solve by any direct procedure. Problems like the one listed above certainly seem to be of this kind, but so far no one has managed to prove that any of them really are so hard as they appear, i.e., that there really is no feasible way to generate an answer with the help of a computer. Stephen Cook and Leonid Levin formulated the P (i.e., easy to find) versus NP (i.e., easy to check) problem independently in 1971.

Image credit: on the left, Stephen Cook by Jiří Janíček (cropped).   CC BY-SA 3.0

Problem Statement. Does P = NP?

\( \mathcal{P} \) = {L | L = L(M) for some Turing machine M that runs in polynomial time}.

The notation \( \mathcal{NP} \) stands for “Nondeterministic Polynomial time”, since originally NP was defined in terms of nondeterministic machines (that is, machines that have more than one possible move from a given configuration).

To put it more briefly, \( \mathcal{P} \) is the set of easy decision problems. \( \mathcal{NP} \) is the class of decision problems for which it is easy to check the correctness of a claimed answer, with the aid of a little extra information. So we aren’t asking for a way to find a solution, but only to verify that an alleged solution really is correct.

In P are the problems where it’s easy to find a solution, and in NP are the problems where it’s easy to check a solution that may have been very tedious to find.

\(\huge \mathbf{\mathbb{P}} \subset \mathbf{\mathbb{NP}} \tt{?}\)

The crucial question related to P and NP is whether the subset relation is proper?

很显然的是P类都属于NP类 (\( \displaystyle {\mathsf {P}}\subseteq {\mathsf {NP}} \)),这样问题就变成:是否某些NP类问题(比如旅行推销员问题)不存在多项式时间过程算法? 问题难在如何证明这一点?如何从逻辑上排除旅行推销员问题存在多项式时间过程算法的可能性?这跟黎曼假设的严格数学证明要排除临界线外有复零点的可能性一样困难。

复杂性范畴之间的包含关系:

\(\mathrm{LOGSPACE} \subseteq \mathbf{P} \subseteq \mathbf{NP} \subseteq \mathrm{PSPACE} \)
\(\mathrm{LOGSPACE} \subset \mathrm{PSPACE} \)

LOGSPACE ⊆ P ⊆ NP ⊆ PSPACE,用简单的对角化论证法能够证明 LOGSPACEPSPACE 的真子集,其余的包含关系是否为真包含关系?无人能够证明。

很多研究复杂性理论的专家都认为 P≠NP,Scott Aaronson 从哲学角度发表了他的看法:“如果P = NP,那么我们就会处于一个截然不同的世界中,创造性飞跃将失去其难能可贵的价值,解决一个问题和认识到问题有解没什么分别。任何一个能欣赏交响乐的人都能成为莫扎特,任何一个能理解数学证明的人都能成为高斯…”。(If P = NP, then the world would be a profoundly different place than we usually assume it to be. There would be no special value in “creative leaps,” no fundamental gap between solving a problem and recognizing the solution once it’s found. Everyone who could appreciate a symphony would be Mozart; everyone who could follow a step-by-step argument would be Gauss…)。按 Stephen Cook 在白皮书中的说法,如果P = NP,那么机器就能给出克雷数学研究所七大难题的形式化证明,尽管证明会很繁琐很冗长。( If P = NP, …. For example, it would transform mathematics by allowing a computer to find a formal proof of any theorem that has a proof of reasonable length, since formal proofs can easily be recognized in polynomial time. Such theorems may well include all of the CMI prize problems. Although the formal proofs may not be initially intelligible to humans, the problem of finding intelligible proofs would be reduced to that of finding a recognition algorithm for intelligible proofs. Similar remarks apply to diverse creative human endeavors, such as designing airplane wings, creating physical theories, or even composing music. The question in each case is to what extent an efficient algorithm for recognizing a good result can be found. This is a fundamental problem in artificial intelligence, and one whose solution itself would be aided by the NP-solver by allowing easy testing of recognition theories. )

在克雷数学研究所的官方资料中,此问题由 Stephen Cook (创造性地提出 NP-complete 概念并证明The satisfiability problem是NP-完全问题)作出权威解读。

相关的经典论文

数学元素

NP-hard 问题

专家评论

  • Richard Karp: (Berkeley, unsure, P!=NP) My intuitive belief is that P is unequal to NP,but the only supporting arguments I can offer are the failure of all efforts to place specific NP-complete problems in P by constructing polynomial-time algorithms.I believe that the traditional proof techniques will not suffice. Something entirely novel will be required. My hunch is that the problem will be solved by a young researcher who is not encumbered by too much conventional wisdom about how to attack the problem.
  • Donald Knuth: (Retired from Stanford) It will be solved by either 2048 or 4096. I am currently somewhat pessimistic. The outcome will be the truly worst case scenario: namely that someone will prove “P=NP because there are only finitely many obstructions to the opposite hypothesis”; hence there will exists a polynomial time solution to SAT but we will never know its complexity!
  • Alexander Razborov: (Institute for Advanced Study) Well, this problem is (at the moment) very unique since we seem to be missing even the most basic understanding of the nature of its difficulty. Neither we currently have any ideas of what approach might turn out to be viable and lead us to the solution (or at least to a better understanding of the problem). All approaches tried so far probably (in some cases, provably) have failed. In this sense P=NP is different from many other major mathematical problems on which a gradual progress was being constantly done (sometimes for centuries) whereupon they yielded, either completely or partially. Perhaps, although, the key word in this difference is “century”, and the P=NP problem has simply not aged enough yet (by mathematical standards).
  • Bob Tarjan: (Princeton) In my view, there is no way to even make intelligent guesses about the answer to any of these questions. If I had to bet now, I would bet that P is not equal to NP. I estimate the half-life of this problem at 25 - 50 more years, but I wouldn’t bet on it being solved before 2100. Its solution will require unforeseen new techniques.
  • Avi Wigderson: (Institute of Advanced Study) I think this project is a bit premature. I think we know too little of what is relevant to even guess answers to your questions, certainly if “we” is replaced by “I.”The only thing I can definitely say, is that it is one of the most important and interesting questions ever asked by humans, and more people and resources should participate in filling up the holes that would allow better guesses of answers to your questions.
  • 姚期智: It’s hard to say when the question will be resolved. I don’t have even an educated guess. Probably the resolution is that P is not equal to NP. I think the mathematical techniques used will be beautiful.
  • Scott Aaronson: Look, this question of whether P=NP, what can I say? People like to describe it as “probably the central unsolved problem of theoretical computer science.” That’s a comical understatement. P vs. NP is one of the deepest questions that human beings have ever asked. And not only that: it’s one of the seven million-dollar prize problems of the Clay Math Institute! What an honor! Imagine: our mathematician friends have decided that P vs. NP is as important as the Hodge Conjecture, or even Navier-Stokes existence and smoothness! (Apparently they weren’t going to include it, until they asked around to make sure it was important enough.) Dude. One way to measure P vs. NP’s importance is this. If NP problems were feasible, then mathematical creativity could be automated. The ability to check a proof would entail the ability to find one. Every Apple II, every Commodore, would have the reasoning power of Archimedes or Gauss. So by just programming your computer and letting it run, presumably you could immediately solve not only P vs. NP, but also the other six Clay problems. (Or five, now that Poincaré is down.) Well, we certainly believe P≠NP. Indeed, we don’t even believe there’s a general way to solve NP problems that’s dramatically better than brute-force search through every possibility. The central challenge any P≠NP proof will have to overcome is to separate the NP problems that really are hard from the ones that merely look hard.

观点

  • P vs. NP 问题的解决可能有赖于大幅度创新的证明技术
  • 堵丁柱 & 葛可一的《计算复杂性导论》前言:
    • 人们在七十年代开始对NP-完全问题的研究主要是横向发展,也就是以许多不同的计算模型来分析难解问题的本质。这些新的计算模型包括了平行计算模型、概率计算模型、布尔线路、判断树、平均复杂性、交互证明系统以及程式长度复杂性等等。对这些新的计算模型的研究一方面使我们对难解问题有了更深一层的认识,一方面也产生了一些预想不到的应用。最显著的一个例子就是计算密码学的革命性突破:基于 NP 问题的公钥密码体系。另一个有名的例子是线性规划的多项式时间解的发现。
    • 到了八十年代中,对NP-完全问题的研究有了纵向的突破,在许多表面看来并不相关的计算模型之间发现了深刻的刻划关系。这些刻划关系不但解决了几个令人困扰多年的未解问题,同时也刺激了其它相关领域的发展。其中之一是对线路复杂性的研究发现了一些问题在某种有限制的线路模型中必有指数下界。这些结果使用了组合数学与概率方法等新的数学工具,并且解决了一个有名的有关多项式分层的未解问题。另一个更重大的结果是以概率可验证明对 NP 类的刻划。由此得出了许多组合优化问题近似解的NP-完全性,从而刺激了算法界对近似算法研究的新热潮。这个结果来自于对交互证明系统这个概念的扩展,并且使用了线性代数与编码理论等数学证明技巧。
  • 1993年 RazborovRudich 证明的一个结果表明,给定一个特定的可信的假设,在某种意义下「自然」的证明不能解决 P vs NP 问题。这表明一些现在似乎最有希望的方法不太可能成功。随着更多这类的定理得到证明,该定理的可能证明有越来越多的陷阱要规避。
  • In 1994, Alexander Razborov and Steven Rudich discovered the natural proofs barrier, which explained why previous attempts to prove P ≠ NP had failed.
  • Razborov and Rudich then proved their main result: A natural proof of P ≠ NP would require a very comprehensive understanding of how easy-to-compute and hard-to-compute functions differ, and that knowledge could also fuel a fast algorithm for spotting easy-to-compute functions even if they’re superficially complicated. If complexity theorists had succeeded in a natural proof of P ≠ NP, they would have discovered a nearly infallible way to glance at an arbitrary truth table and determine whether the corresponding function had high or low circuit complexity — a much stronger and more general result than they had set out to prove.
  • The first attempts at determining the relationship between P and NP used an elegant trick called diagonalization that had been essential for other major results in computer science. But researchers soon realized that any diagonalization-based solution to the P versus NP problem would be self-contradictory. Researchers concluded that they’d need new techniques to make progress.

Substring versus Subsequence

  • Substring : A pattern P is called a substring of Text T if the pattern appears in the Text in a continuous fashion.
  • Subsequence : A pattern P is called a subsequence of Text T if the pattern preserves the relative ordering of characters within the text T and it might not appear in a continuous fashion. e.g., the prime numbers are a subsequence of the positive integers.

  • Subsequence is a generalisation of substring, suffix, and prefix. Finding the longest string which is a subsequence of two or more strings is known as the longest common subsequence problem.

Example: The string “anna” is a subsequence of the string “banana”:

1
2
3
banana
|| ||
an na
  • Substring

A substring of a string is a prefix of a suffix of the string, and equivalently a suffix of a prefix. If one string is a substring of another, it is also a subsequence, which is a more general concept.

Example: The string “ana” is a substring (and subsequence) of banana at two different offsets:

1
2
3
4
5
banana
|||||
ana||
|||
ana