您的位置:首页 > 运维架构 > Linux

【面试】58同城-Linux后台开发实习生

2015-08-24 12:12 721 查看
昨天下午去面试了58同城的Linux后台开发实习生,发挥的很不好,很多基础问题都答不上来。马上要校招了,还是要加强基础学习啊!血的教训,基础非常重要!!!

1.写出二叉树的深度优先遍历和广度优先遍历。

这其实是一道特别基础的算法题,博主当时是用队列实现的广度遍历,用递归实现的深度遍历。后来面试官又问我,不用递归怎么来实现深度遍历,当时二了,没有想过来,其实和广度遍历类似,用栈来实现就好了。代码:

struct Tree
{
	Tree *left;
	Tree *right;
	int val;
	Tree(int x):val(x),left(NULL),right(NULL) {}
};

void width(Tree *root)
{
	if (root == NULL)
	{
		return;
	}
	queue<Tree *> nodes;
	Tree *temp;
	nodes.push(root);
	while(!nodes.empty())
	{
		temp = nodes.front();
		nodes.pop();
		cout<<temp->val<<"\t";
		if (temp->left)
		{
			nodes.push(temp->left);
		}
		if (temp->right)
		{
			nodes.push(temp->right);
		}
	}
}

void depth(Tree *root)
{
	if (root == NULL)
	{
		return;
	}
	cout<<root->val<<"\t";
	depth(root->left);
	depth(root->right);
}

void depthByStack(Tree *root)
{
	if (root == NULL)
	{
		return;
	}
	stack<Tree *> nodes;
	Tree *temp;
	nodes.push(root);
	while(!nodes.empty())
	{
		temp = nodes.top();
		nodes.pop();
		cout<<temp->val<<"\t";
		if (temp->right)
		{
			nodes.push(temp->right);
		}
		if (temp->left)
		{
			nodes.push(temp->left);
		}
	}
}


2.STL中map实现的原理,以及map中[]符号的查找操作。

这个问题回答的并不好,回来也恶补了一下map的知识。STL中map和vector还是要重点掌握啊,这里推荐两篇博文:1)STL中map用法详解,http://blog.csdn.net/it_yuan/article/details/22697205;2)小心使用STL中map的[]操作符,http://blog.csdn.net/starlee/article/details/2256054。

3.C++中const的用法。

推荐文章:http://www.cnblogs.com/xudong-bupt/p/3509567.html

4.写一个多态的demo。

当时博主就傻了,不知道怎么写了。。。当时主要是懵了,其实可以写一个简单的函数多态,比如下面这个小例子:

int add (int num1,int num2)
{
	return num1+num2;
}

int add (int num1,string str1)
{
	return num1+atoi(str1.c_str());
}

int _tmain(int argc, _TCHAR* argv[])
{
	int a1 = 1;
	int a2 = 2;
	string str ="2";
	cout<<"函数多态add(int num1,int num2)的结果是:"<<add(a1,a2)<<endl;
	cout<<"函数多态add(int num1,string str1)的结果是:"<<add(a1,str)<<endl; 
	return 0;
}


感悟:自己的基础知识还是太差,一定要恶补自己的基础知识。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: