由于3月5号要进行Epic公司的Online Assessment,准备刷些面经做准备,毕竟这是我的第一次比较重要的面试。。
这是我准备Epic技术面试的小专栏。。。希望面试的时候能做够30题+吧,这样做OA的时候会轻松一点。
目前完成:29题
/*************************************************我是分割线*******************************************************/
虽然准备的比较充分了,但是还是悲剧了。。
原因可能有以下几点:
1,确实招人快招满了
2,电话面试的设计题答的一坨屎。。另外三哥哥的口音仍然听不懂
3,题做的还是不够多
不管怎样,仍然努力刷题+提升自己背景双管齐下吧。。。提升背景是为了拿到更多面试,刷题是为了在面试中发挥的更优秀。Epic是我半年北美求职之路的开始。希望在今年十月毕业的时候能有好的offer吧!
/*********************************************************************************************************************/
Epic 01 Colorful Number
本题题目要求如下:
/* Description: Determine whether a number is colorful or not.
* 263 is a colorful number because (2,6,3,2x6,6x3,2x3x6) are all different
* whereas 236 is not because (2,3,6,2x3,3x6,2x3x6) have 6 twice.
* So take all consecutive subsets of digits, take their product
* and ensure all the products are different */
我对这个的算法就是两次循环
第一次:顺序加入2,6,3
每次都检查
第二次:
检查2 * 6, 2 * 6 * 3,
检查6 * 3
实现代码如下:
# include <iostream>
# include <vector>
# include <set>
# include <string>
using namespace std;
bool color(int);
int main(int argc, char** argv) {
// cout << color(263) << endl;
cout << color(236) << endl;
// cout << color(40) << endl;
// cout << color(41) << endl;
// cout << color(2357496) << endl;
// cout << color(2634) << endl;
// cout << color(0) << endl;
// cout << color(9) << endl;
return 0;
}
bool color(int num) {
if (num < 0)
return false;
set<int> s;
string str = to_string(num);
int color = str[0] - '0';
s.insert(color);
for (int i = 1; i < str.length(); ++i) {
int n = str[i] - '0';
cout << "check: " << n << endl;
if (s.find(n) != s.end())
return false;
else
s.insert(n);
}
for (int i = 0; i < str.length(); ++i) {
color = str[i] - '0';
for (int j = i + 1; j < str.length(); ++j) {
int n = str[j] - '0';
color *= n;
cout << "check: " << color << endl;
if (s.find(color) != s.end())
return false;
else
s.insert(color);
}
}
return true;
}
Epic 02 Well ordered password
本题题目要求如下:
/* Description: Find all the possible passwords, given the length of the password and that
* it is a well ordered number (159 is well-ordered as 1<5<9) */
这题就有点类似dfs了,
举个简单的例子:
下面是我画的一张图:
下面的就是假设密码是0-4然后密码只有2位的解。
由于第一层时,还没有数字,所以剩余2位,则最大数字是5-2=3.最小数字是parent + 1,但是没有parent,所以是0
第二层,最小可取的是parent + 1,最大是 5 - 1 = 4
这个基本就跟recursive解dfs一样,代码如下:
# include <iostream>
# include <vector>
using namespace std;
void well_ordered(int, int, int);
vector<int> res;
int main(int argc, char** argv) {
well_ordered(0, 0, 4);
for (int i = 0; i < res.size(); ++i)
cout << res[i] << endl;
}
void well_ordered(int num, int start, int remain) {
if (remain == 0)
res.push_back(num);
else {
for (int i = start + 1; i <= (10 - remain); ++i) {
well_ordered(num * 10 + i, i, remain - 1);
}
}
}
Epic 03 Spiral order
题目要求如下:
/* Description: If an N X N matrix is given, print it in spiral order.
* Example: Below is 5 X 5 matrix
* i l o v e
* d i n t e
* n i e e p
* a v w r i
* m a x e c
* Print in spiral order. Output is iloveepicexamandinterview */
本题曾经在leetcode中做过,我的博客里面也有这道题的分析,这里就不重复了,直接上传代码:
# include <iostream>
# include <vector>
using namespace std;
vector<char> spiral_order(vector<vector<char>>);
int main(int argc, char** argv) {
vector<vector<char>> str = {
{'i', 'l', 'o', 'v', 'e'},
{'d', 'i', 'n', 't', 'e'},
{'n', 'e', 'w', 'e', 'p'},
{'a', 'i', 'v', 'r', 'i'},
{'m', 'a', 'x', 'e', 'c'}
};
vector<char> res = spiral_order(str);
for(int i = 0; i < res.size(); ++i)
cout << res[i];
cout << endl;
return 0;
}
vector<char> spiral_order(vector<vector<char>> str) {
vector<char> res;
int col_start = 0;
int col_end = 4;
int row_start = 0;
int row_end = 4;
while (true) {
/* go right */
for (int i = col_start; i <= col_end; ++i)
res.push_back(str[row_start][i]);
row_start += 1;
if (res.size() == 25)
break;
/* go down */
for (int j = row_start; j <= row_end; ++j)
res.push_back(str[j][col_end]);
col_end -= 1;
if (res.size() == 25)
break;
/* go left */
for (int i = col_end; i >= col_start; --i)
res.push_back(str[row_end][i]);
row_end -= 1;
if (res.size() == 25)
break;
/* go up */
for (int j = row_end; j >= row_start; --j)
res.push_back(str[j][col_start]);
col_start += 1;
if (res.size() == 25)
break;
}
return res;
}
Epic 04 Allowed password
本题要求如下:
/* Description: There is a security keypad at the entrance of a building.
* It has 9 numbers 1 - 9 in a 3x3 matrix format.
* 1 2 3
* 4 5 6
* 7 8 9
* The security has decided to allow one digit error for a person but that digit
* should be horizontal or vertical. Example: for 5 the user is allowed to enter
* 2, 4, 6, 8 or for 4 the user is allowed to enter 1, 5, 7. IF the security code to
* enter is 1478 and if the user enters 1178 he should be allowed. Write a function
* to take security code from the user and print out if he should be allowed or not */
这题难度并不高,如果在leetcode里面,就是个简单题水平。。而且因为最多检查一次是否是该数字上下左右,剩下的时候只要单纯比较是不是一样就行了。。
那唯一一次比较我的处理是把user输入和原密码在keypad的位置计算出来,然后比较他们是不是相邻关系。。
代码如下:
# include <iostream>
# include <vector>
using namespace std;
const vector<int> pwd = {1, 4, 7, 8};
const vector<vector<int>> keypad = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
bool pwd_allowed(vector<int>);
int main(int argc, char** argv) {
vector<int> user = {1, 4, 7, 8};
cout << "True or False: " << pwd_allowed(user) << endl;
return 0;
}
bool pwd_allowed(vector<int> user) {
bool mistake = false;
for (int i = 0; i < user.size(); ++i) {
if (user[i] == pwd[i])
continue;
else {
if (mistake == true)
return false;
else {
mistake = true;
int p_row;
int p_col;
int u_row;
int u_col;
/* find the posistion of the pwd[i] */
for (int j = 0; j < keypad.size(); ++j) {
for (int k = 0; k < keypad[0].size(); ++k) {
if (pwd[i] == keypad[j][k]) {
p_row = j;
p_col = k;
}
if (user[i] == keypad[j][k]) {
u_row = j;
u_col = k;
}
}
}
/* 4 condition */
if ((p_row == u_row and p_col == u_col - 1)
or (p_row == u_row and p_col == u_col + 1)
or (p_col == u_col and p_row == u_row - 1)
or (p_col == u_col and p_row == u_row + 1))
;
else
return false;
}
}
}
return true;
}
Epic 05 Continuous alphabets
题目要求如下:
/* Description: Print continuous alphabets from a sequence of arbitrary alphabets
* For example:
* Input: abcdefljdflsjflmnopflsjflasjftuvwxyz
* Output: abcdef; mnop; tuvwxyz
* Input: AbcDefljdflsjflmnopflsjflasjftuvWxYz
* Output: abcdef; mnop; tuvwxyz */
本题算法第一步就是将所有的都转为lower case,然后开始计算
如果和前面的字母相同,则在str 加上这个字母,如果不同,则看是否str已经存储大于1个元素,如果大于1个,则将其存入vector中,否则不作处理,但是无论如何都要以当前的char建立一个新的str
详细代码如下:
# include <iostream>
# include <vector>
# include <string>
using namespace std;
vector<string> con_alpha(string);
int main(int argc, char** argv) {
string input = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
for (int i = 0; i < input.length(); ++i)
input[i] = tolower(input[i]);
cout << input << endl;
vector<string> res = con_alpha(input);
for (int i = 0; i < res.size(); ++i)
cout << res[i] << endl;
return 0;