当前位置:网站首页>Tester's algorithm interview question - find mode
Tester's algorithm interview question - find mode
2022-07-04 23:43:00 【Code classmate software test】
Algorithm interview — Find the mode
Premise :
Now the interview of test engineer , More or less, I will ask about programming technology . In programming technology , Often choose a simple algorithm problem . Many students see this , Often I don't know what to do . If the consequences are light, the salary will be reduced , The most important thing is to lose this interview .
In fact, the algorithm in the interview , You can brush questions to prepare . Now share the algorithm interview questions encountered in the recent interview
subject - Find a mode in the list
Given a size of n Array of , Find the mode
* The number of : It means that the number of occurrences in the array is greater than "n/2" The elements of
Example :
Given array :list = [1,2,5,5,5,5,7]
Expected output : 5
LV1: Direct problem solving
First use the most direct way , Try to change the topic
def func(num:list): # Define a function , receive " list " data
res = [] # Define a variable to receive the result
For greater than "n/2" This demand , You can find out first n
n = len(num) # use len Function to get n
Find the mode in the array , It can be understood as : Compare circularly in the array .
On the condition that The number of occurrences of the current element (list.count() Function can get ) Greater than n/2
If you qualify , Then save it to res variable
For greater than "n/2" This demand , You can find out first n
for i in num: # Traverse num
now_time = num.count(i) # Get the number of occurrences of the current traversal item
if now_time > n/2: # If the number appears more than n/2
res.append(i) # Then add the result
Due to circulation , Each mode will be added to the result to get the following result
[5, 5, 5, 5]
So we are adding one to duplicate the data
return set(res)
result :
Lv2: simplify
In the above scheme , Strive to achieve the requirements quickly . Redundant in details , Here is a step of simplification .
def func(num:list): # Define a function , receive " list " data
# res = [] # In fact, due to the rule of mode ( Number > commonly ), There can only be one in an array . So it's good to return directly
# n = len(num) # Put this calculation directly into if In the condition
for i in num: # Traverse num
# now_time = num.count(i) # You can put this calculation directly on the condition
if num.count(i) > len(num)/2: # Can be n/2 The calculation of , Put it directly here
return i # Then add the result
The effect is as follows :
LV3 Efficiency optimization
From a functional point of view , The above scheme is feasible . But once you encounter massive data , Repeat the calculation list.count() It's very time consuming . Pictured :
This test data includes 5w+ Elements , The calculation is huge
Rethink the mode in the topic , Found several features :
① At most, there are 1 Multiple modes ( There will be no 2 More than half of the elements )
② If the array is arranged from large to small (list.sort() Function can sort ), Then the number in the middle must be mode
③ For arrays , There are also odd numbers / Even elements
If it's odd
The number of elements of the array is odd
l = [1,1,3,3,3]
print(len(l) // 2) have access to " to be divisible by " Get the midpoint
At this time, it happens to get to the midpoint of the array , Then you must get the mode
If it's even
The number of elements of the array is even
l = [1,1,3,3,3,3]
print(len(l) // 2)
The absolute midpoint is rounded back , This case is an example 4/7
At this time, the mode must also be >7/2, That is, at least 4 Time
therefore l[len(2//l)] It must be the mode
rewrite :
The number of elements of the array is even
def majorityElement(nums):
nums.sort()
return nums[len(nums) // 2]
The effect is as shown in the picture :
Conclusion : The speed is faster than the direct solution , fast 20 More than times
边栏推荐
- 如果炒股开华泰证券的户,在网上开户安全吗?
- Question brushing guide public
- ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
- The pit of sizeof operator in C language
- 【雅思阅读】王希伟阅读P3(Heading)
- Application of fire fighting system based on 3D GIS platform
- Advanced template
- [JS] - [dynamic planning] - Notes
- Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
- Compare two vis in LabVIEW
猜你喜欢
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
How to use fast parsing to make IOT cloud platform
Etcd database source code analysis - brief process of processing entry records
图解网络:什么是网关负载均衡协议GLBP?
如何有效对直流列头柜进行监测
【js】-【排序-相关】-笔记
How to apply for PMP project management certification examination?
"Xiaodeng" domain password policy enhancer in operation and maintenance
The initial trial is the cross device model upgrade version of Ruijie switch (taking rg-s2952g-e as an example)
随机推荐
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
Advantages of Alibaba cloud international CDN
C language to quickly solve the reverse linked list
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
【js】-【排序-相关】-笔记
【雅思阅读】王希伟阅读P4(matching1)
Ffmpeg quick clip
Stm32 Reverse Introduction to CTF Competition Interpretation
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Using the uniapp rich text editor
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
Financial markets, asset management and investment funds
How to apply for PMP project management certification examination?
js正则表达式之中文验证(转)
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
Excel shortcut keys - always add
[JS] - [sort related] - Notes
45岁教授,她投出2个超级独角兽
Compare two vis in LabVIEW
企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)