挑战C++服务器开发岗笔试题

最近指导了一位同学,他获得某公司的C++服务器开发岗的笔试机会。要求线上答题,是开放式答题,只要在规定时间内把题目做好提交即可。和我沟通指导后,很顺利的通过了笔试。现在把整个指导过程记录一下,以作分享。

笔试题原文

In this project, we want to create a cross reference generator. We assume that we have a file of text(GNU GENERAL PUBLIC LICENSE is a good candidate). We want to read in the file and print out an alphabetized list of all the words in the file, using the following format:

WORD

NUMBER OF OCCURRENCES

LINES THAT THE WORD APPEARED ON

You may assume that your cross-reference generator is not case-sensitive, that is the words “Did” and “did” are the same. And again you can use Pseudo Code (some sample pseudo code is on Page 4) or Lua if you can complete in time. At least explain your idea.

For example, running the cross-reference generator with the first two lines from the poem
Jabberwocky by Lewis Carroll produce the following:

Line 1 Twas brillig and the slithy toves
Line 2 Did gyre and gimble in the wabe

Word

Count

Lines

and

2

1   2

brillig

1

1

did

1

2

gimble

1

2

gyre

1

2

in

1

2

slithy

1

1

the

2

1   2

toves

1

1

twas

1

1

wabe

1

2

Total

13

以上就是全部问题。

真题分析

题目其实并不很难,技术经理出这个题目,大概是想考核两个“技术点”。

第一点,就是怎样处理英文文档的项目需求。国内程序员,大部分的英语水平非常一般,如果能保持高中生的英语水平,已经算是程序员中的“佼佼者”了,但是对于程序员老鸟,这并不妨碍处理英文文档。直接使用网络工具,加项目理解,基本上就能理解了。推荐工具,使用有道词典。需要注意的是,翻译工具翻译后只能作为参考,需要程序员结合语境进行“二次翻译”。

第二点,就是使用C++解决问题的能力。这个题目,难度并不大,但是如果对C++的各种基础容器不是很熟悉的话,直接使用C语言“裸写“,可能会导致时间上不够用,最后感叹这个题自己会做,只是出了点小问题,没有做完,没有做好。

真题解答

#include <iostream>
#include <list>	
#include <vector>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

struct Node {
	string word;
	int count = 0;
	vector<int>lines;

	bool operator==(const Node& other)const {
		return  _stricmp(word.c_str(), other.word.c_str()) == 0;
	}
	friend ostream& operator<<(ostream &out, Node &word)  {
		out << setw(10) <<std::left  << word.word << "\t" << word.count << "\t";
		for (int i = 0; i < word.lines.size(); i++) {
			out << word.lines[i] << "   ";
		}
		return out;
	}
};

bool wordNodeCompare(Node& node1, Node& node2) {
	return  _stricmp(node1.word.c_str(), node2.word.c_str()) < 0;
}

int main(void) {
	ifstream fin;
	string line;
	list<Node> wordList;
	int lineNumer = 0;
	int count = 0;
	const char* dStr = " ,.'-\"()1234567890";

	fin.open("test.txt");
	while (!fin.eof()) {
		getline(fin, line);
		lineNumer++;
		char* tmpLine = new char[line.length() + 1];
		strcpy(tmpLine, line.c_str());
		char* p = strtok(tmpLine, dStr);
		while (p) {
			Node word;
			word.word = p;
		
			auto it = std::find(wordList.begin(), wordList.end(), word);
			if (it != wordList.end()) {
				it->count++;
				if (std::find(it->lines.begin(), it->lines.end(), lineNumer) == it->lines.end()) {
					it->lines.push_back(lineNumer);
				}
			}
			else {
				word.count = 1;
				word.lines.push_back(lineNumer);
				wordList.push_back(word);
			}

			p = strtok(NULL, dStr);
		}
	}

	wordList.sort(wordNodeCompare);


	cout << setw(10) << std::left << "Word" << "\tCount\tLines" << endl;
	cout << "----------------------------------------------------" << endl;
	count = 0;
	for (auto it = wordList.begin(); it != wordList.end(); it++) {
		cout << *it << endl;
		count += it->count;
	}
	cout << "----------------------------------------------------" << endl;
	cout << setw(10) << std::left << "Total" << "\t" << count << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/pzjdoytt/article/details/125585915