当前位置:网站首页>My friend sent me some interview questions
My friend sent me some interview questions
2022-07-28 13:41:00 【Half root blue white】
My friend sent me some interview questions
The first question is
You want to spend your next vacation in a foreign country. In the summer you are free for N consecutive days. You have consulted Travel Agency and learned that they are offering a trip to some interesting location in the country every day. For simplicity, each location is identified by a number from 0 to N - 1. Trips are described in a non-empty array A: for each K (0 <K< N), A[K] is the identifier of a location which is the destination of a trip offered on day K. Travel Agency does not have to offer trips to all locations, and can offer more than one trip to some locations.
You want to go on a trip every day during your vacation. Moreover, you want to visit all locations offered by Travel Agency. You may visit the same location more than once, but you want to minimize duplicate visits. The goal is to find the shortest vacation (a range of consecutive days) that will allow you to visit all the locations offered by Travel Agency.
For example, consider array A such that:
A[0]=7
A[1]=3
A[2]=7
A[3]=3
A[4]=1
A[5]=3
A[6]=4
A[7]=1
Travel Agency offers trips to four different locations (identified by numbers 1, 3, 4 and 7). The shortest vacation starting on day 0 that allows you to visit all these locations ends on day 6 (thus is seven days long). Travel Agency offers trips to four different locations (identified by numbers 1, 3, 4 and 7). The shortest vacation starting on day 0 that allows you to visit all these locations ends on day 6 (thus is seven days long). However, a shorter vacation of five days (starting on day 2 and ending on day 6) also permits you to visit all locations. On every vacation shorter than five days, you will have to miss at least one location.
Write a function:
class Solution { public int solution(int[] A); } that, given a non-empty array A consisting of N integers, returns the length of the shortest vacation that allows you to visit all the offered locations.
For example, given array A shown above, the function should return 5, as explained above.
Given A = [2, 1, 1, 3, 2, 1, 1, 3], the function
It's dense I know all the letters Connect I don't know what happened !
def solution(a):
pass
The second question is
Jack has completed his very first programming test and is now wondering about his score. He received an email containing the final report about the task he solved in the test, but it omitted the score. Nathan never gives up, so he decided to compute his result from the report. He found out that:
his program was tested on several test cases;every test case has given one of the following results: passed, failed, timeout, error;test cases were organized into groups numbered by consecutive natural numbers;his program scored points for a group only when the result of every test case in the group was “passed”.
In the report, test cases are named according to the following rule: if the test case is the only test case in the group, its name is [task name] + [group number]. If there is more than one test in the group, its name is extended by a lowercase English letter which is different for each test case. Example test case names are: test1, test2a, test2b. In this example, test2a and test2b form one group and test1 forms another. Jack’s score is the number of groups his program passed multiplied by 100 and divided by the total number of groups. If the result is fractional, the number of points is rounded to the highest integer not greater than the result.
For example, if the program passed two groups out of three, the result is 2 * 100 / 3 = 66.(6), so Nathan would gain 66 points.
Write a function
solution(String[] n, String[])that, given the names and results of all the test cases, returns the number of points Jack scored. n and r are arrays (containing strings). n[i] is the case name; r[i] is the result;
for example
n = [‘test1’,‘name2a’,‘name2b’,‘haha3a’,‘haha3b’,‘haha3c’,‘jack4a’,‘jack4b’];
r = [‘passed’,‘failed’,‘passed’,‘passed’,‘passed’,‘passed’,‘timeout’,‘failed’];We have 4 groups, and only 2 groups passed the test, thus the result is 50;
def solution(n, r):
_list = [chr(i) for i in range(97, 123)]
group_dict = {
}
for index, item in enumerate(n):
if item[-1] in _list:
group_name = item[:-1]
else:
group_name = item
temp = r[index] == 'passed'
if group_name in group_dict:
group_dict[group_name].append(temp)
else:
group_dict[group_name] = [temp]
passed = len([1 for item in group_dict.values() if all(item)])
groups = len(group_dict.keys())
return passed*100//groups
Third question
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds’ scores.
At the beginning of the game, you start with an empty record. You are given a list of strings ops, where ops[i] is the ith operation you must apply to the record and is one of the following:
An integer x - Record a new score of x.
“+” - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
“D” - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
“C” - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.
Return the sum of all the scores on the record. The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: ops = [“5”,“2”,“C”,“D”,“+”]
Output: 30
Explanation:
“5” - Add 5 to the record, record is now [5].
“2” - Add 2 to the record, record is now [5, 2].
“C” - Invalidate and remove the previous score, record is now [5].
“D” - Add 2 * 5 = 10 to the record, record is now [5, 10].
“+” - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.Example 2:
Input: ops = [“5”,“-2”,“4”,“C”,“D”,“9”,“+”,“+”]
Output: 27
Explanation:
“5” - Add 5 to the record, record is now [5].
“-2” - Add -2 to the record, record is now [5, -2].
“4” - Add 4 to the record, record is now [5, -2, 4].
“C” - Invalidate and remove the previous score, record is now [5, -2].
“D” - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
“9” - Add 9 to the record, record is now [5, -2, -4, 9].
“+” - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
“+” - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.Example 3:
Input: ops = [“1”,“C”]
Output: 0
Explanation:
“1” - Add 1 to the record, record is now [1].
“C” - Invalidate and remove the previous score, record is now []
def solution(s:list):
operator = ['+', 'D', 'C']
res = []
for item in s:
if item in operator:
if item == '+':
res.append(sum(res[-2:]))
if item == 'D':
res.append(2*res[-1])
if item == 'C':
res.pop(-1)
else:
res.append(int(item))
return sum(res)
To sum up
I am a Vegetable force , Finally he told me , The first question is the most difficult !30 branch , second 、 The third question is 20 branch ; After I glanced at the first question, I regretted talking big with him .
Last sit , First I looked at the third question , Write the handwritten code when you don't understand the title ,20 More minutes or so Finally, it's written ! Then I answered a phone , Start the second question ,
It's a tough process , It shouldn't have been wrong , Only finally did I find Is the wrong variable name , Because I seldom touch TDD Development model , Very uncomfortable The error information is not clear enough !
But at the last second There is a bracket I don't know whether to delete , Don't talk big anymore . Or do more algorithm problems , The latter kind of thinking should be expanded !
When writing the article, I reviewed Still dare not look at the first question , however If it is a Chinese question , Now I think the latter two questions are a little too simple , Or I think it's too simple !
边栏推荐
- jar包
- 基于神经网络的帧内预测和变换核选择
- You have to apologize if you get involved in the funny shop?
- One track education, PHP training, unity of knowledge and practice, popular
- Leetcode-190. inverting binary bits
- 我抄底了被清算的NFT,却被OpenSea上了锁
- Resolve browser password echo
- Compare the new and old data to find the added and deleted ones
- MySQL practice -- master-slave replication
- 数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
猜你喜欢

Realize the mutual value transfer between main window and sub window in WPF

Guide for using IP phone system and VoIP system

屈辱、抗争、逆转,三十年,中国该赢微软一次了

I copied the bottom of the liquidated NFT, but was locked by opensea

.NET桌面开发的一些思考

gicv3 spi register

Night God simulator packet capturing wechat applet
![[dark horse morning post] byte valuation has shrunk to $270billion;](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing

111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误

我抄底了被清算的NFT,却被OpenSea上了锁
随机推荐
Jar package
Tidb 6.x in action was released, a summary of 6.x practices that condense the collective wisdom of the community!
《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
功率放大器和匹配网络学习
[ecmascript6] symbol and its related use
为什么说Crypto游戏正在改变游戏产业?
Change password, confirm password verification antd
C language: optimized merge sort
leetcode-136.只出现一次的数字
C language: merge sort
powerdesigner创建数据库模型(概念模型举例)
Unity - "synthetic watermelon" small game notes
半波整流点亮LED
数据库系统原理与应用教程(061)—— MySQL 练习题:操作题 21-31(五)
Leetcode-136. numbers that appear only once
GameStop熊市杀入NFT交易,老牌游戏零售商借Web3焕发第二春
Beyond istio OSS -- current situation and future of istio Service Grid
如何配置adb环境变量(环境变量在哪打开)
Realize the mutual value transfer between main window and sub window in WPF
C语言:优化后的归并排序