二面:
二面先聊自我介绍,简要介绍之前做的项目
问题1. 写代码:判断一个数字序列是BST后序遍历的结果,下面是我现场写的代码,没有测试过
- bool IsPostOrderOfBST(int array[], int low, int high)
- {
- if(low >= high){
- return true;
- }
- int split = -1, i;
- for( i = low; i < high; i++){
- if(split != -1 && array[i] < array[high]){
- return false;
- }
- if(split == -1 && array[i] > array[high]){
- split = i;
- }
- }
- if(split == -1){
- return IsPostOrderOfBST(array, low, high-1);
- }
- return IsPostOrderOfBST(array, low, split-1) && IsPostOrderOfBST(array, split, high-1);
- }
问题2. 写一个单件模式,然后顺便被我引导到扯扯 线程安全 异常安全等话题,我不断完善最初代码使得满足线程安全和异常安全。
下面的代码大概是最初版本:
- #include <stdio.h>
- class Singleton{
- private:
- Singleton(){}
- Singleton(const Singleton &);
- Singleton& operator=(const Singleton&);
- public:
- static Singleton *Instantialize();
- static Singleton *pInstance;
- };
- Singleton* Singleton::pInstance = 0;
- Singleton* Singleton::Instantialize(){
- if(pInstance == NULL){
- pInstance = new Singleton();
- }
- return pInstance;
- }
