前一段时间面过百度商务搜索部门的软件开发实习生,面了3面,没有通过,还差的很远。百度对算法的要求还是比较高的,虽然时间过去了一段时间了,但是有些题目还是可以记起来。特此发篇博客,记录下内容,也以此激励自己,希望下次在去会有进步。
整个面试过程大概写了7 8 道程序题目把,脑袋都写大了。通过这次面试知道了有两个需要注意和锻炼的地方:
1.在纸上写代码的能力。最好带支铅笔和橡皮过去,如果你字写的不好看,写的时候在涂涂画画修改下,会显得代码乱七八糟的,自己看着都觉得恶心。更别提面试官了。如果没有绝对的实力一遍写过,最好用铅笔和橡皮,错了还可以擦掉。
2.在写代码的时候一定要特别注意某些边界条件的判断,尤其要小心。虽说不是什么大错误,但是被面试官发现的话是相当不好的。囧,自己发现了2处。譬如说我在写的时候犯的错误,双链表的最后一个节点的判断条件不是等于空,而是指向第一个头节点。
好了,没有面过就继续努力,吸取下经验教训。继续往前走。下面记录下遇到的程序题目。
#include <iostream> using namespace std; void Swap( int & x, int & y ){ int temp = x; x = y; y = temp; } int partition ( int a[ ], int begin ,int end ){ int temp = a[ begin ]; int i,j; i = begin; j = end; while( i < j ){ while( a[ j ] >= temp && i < j ) j --; if( i < j ){ Swap( a[j], a[ i ] ); i++; } while( a[ i ] <= temp && i < j ) i++; if( i < j ){ Swap( a[ j ], a[ i ] ); j --; } } a[ i ] = temp; return i; } bool isInputInValid = false; bool CheckInvalidArray( int numbers[ ], int length ){ isInputInValid = false;//初始认为输入正确 if( NULL == numbers || length <= 0 ){ isInputInValid = true; } return isInputInValid; } bool CheckMoreThanHalf( int numbers[ ], int length, int result ){ int nums = 0; for( int i = 0; i < length; i ++ ){ if( numbers[ i ] == result ) nums++; } bool isMoreThanHalf = true; if( nums * 2 <= length ){ isInputInValid = true; isMoreThanHalf = false; } return isMoreThanHalf; } int MoreThanHalfNum( int numbers[ ], int length ){ //面试的时候一定要处理参数的输入是否正确 if( CheckInvalidArray( numbers, length ) ) return 0; int middle = length >> 1; int start = 0; int end = length - 1; int partitionIndex = partition( numbers, start, end ); while( partitionIndex != middle ){ if( partitionIndex > middle ){ end = partitionIndex - 1; partitionIndex = partition( numbers, start, end ); } else{ start = partitionIndex + 1; partitionIndex = partition( numbers, start, end ); } } int result = numbers[ middle ]; if( !CheckMoreThanHalf( numbers, length, result ) ) result = 0; return result; } int main() { int num[ ] = {1,2,3,2,2,2,5,4,2}; int result = MoreThanHalfNum( num, 9 ); if( isInputInValid == true ){ if( result == 0) cout << "Input Error,the Arry does not has not half element " << endl; else cout << "Input Error " << endl; } else cout << result << endl; return 0; }在面试的时候,一定要处理参数的输入是否正确。譬如说本题目,需要考虑2点
#include <iostream> using namespace std; bool isInputInValid = false; bool CheckInvalidArray( int numbers[ ], int length ){ isInputInValid = false;//初始认为输入正确 if( NULL == numbers || length <= 0 ){ isInputInValid = true; } return isInputInValid; } bool CheckMoreThanHalf( int numbers[ ], int length, int result ){ int nums = 0; for( int i = 0; i < length; i ++ ){ if( numbers[ i ] == result ) nums++; } bool isMoreThanHalf = true; if( nums * 2 <= length ){ isInputInValid = true; isMoreThanHalf = false; } return isMoreThanHalf; } int MoreThanHalfNum( int numbers[ ], int length ){ //面试的时候一定要处理参数的输入是否正确 if( CheckInvalidArray( numbers, length ) ) return 0; int count = 1; int result = numbers[ 0 ]; for( int i = 1; i < length; i++ ){ if( 0 == count ){ result = numbers[ i ]; count = 1; } if( numbers[ i ] == result ){ count++; } else count--; } if( !CheckMoreThanHalf( numbers, length, result ) ) result = 0; return result; } int main() { int num[ ] = {1,0,3,2,2,2,5,4,2}; int result = MoreThanHalfNum( num, 9 ); if( isInputInValid == true ){ if( result == 0) cout << "Input Error,the Arry does not has not half element " << endl; else cout << "Input Error " << endl; } else cout << result << endl; return 0; }
int LongConsequiveNum( int A[], int length ){ int sum = 0, result = 0; for( int i = 0; i < length; i++ ){ if( A[ i ] > 0 ){ sum += A[ i ]; if( sum > result ) result = sum; } else{ if( A[ i ] + sum <= 0 ){ sum = 0; } else{ sum += A[ i ]; } } } return result ; }
int MaxSubsequenceSum(const int A[],int N) { int ThisSum,MaxSum,j; ThisSum=MaxSum=0; for(j=0;j<N;j++) { ThisSum+=A[j]; if(ThisSum>MaxSum) MaxSum=ThisSum; else if(ThisSum<0) ThisSum=0; } return MaxSum; }
// // MaxSum.cpp // MaxSum // // Created by chenhao on 12/17/13. // Copyright (c) 2013 mini. All rights reserved. // #include <iostream> using namespace std; #define INTMIN -1000 const int MAX_SIZE = 100; int data[ MAX_SIZE + 10 ]; int MaxLen[ MAX_SIZE + 10 ]; int N; int main(int argc, const char * argv[]) { while( cin >> N ){ for( int i = 1; i <= N; i++ ) cin >> data[ i ]; MaxLen[ 1 ] = data[1]; for( int i = 2; i <= N; i++ ){ if ( MaxLen[ i - 1 ] + data[ i ] > data[ i ] ) MaxLen[ i ] = MaxLen[ i - 1 ] + data[ i ]; else MaxLen[ i ] = data[ i ]; } int result = INTMIN; for( int i = 1; i <= N; i++ ) if( result < MaxLen[ i ] ) result = MaxLen[ i ]; cout << result << endl; } }