当前位置:网站首页>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
边栏推荐
- A mining of edu certificate station
- ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
- [JS] - [sort related] - Notes
- Font design symbol combination multifunctional wechat applet source code
- Observable time series data downsampling practice in Prometheus
- IELTS examination process, what to pay attention to and how to review?
- The difference between cout/cerr/clog
- 取得PMP证书需要多长时间?
- 【监控】zabbix
- CTF競賽題解之stm32逆向入門
猜你喜欢
Application of fire fighting system based on 3D GIS platform
Excel shortcut keys - always add
Solution record of jamming when using CAD to move bricks in high configuration notebook
[IELTS reading] Wang Xiwei reading P3 (heading)
【雅思阅读】王希伟阅读P3(Heading)
Compare two vis in LabVIEW
Using the uniapp rich text editor
Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
CTF競賽題解之stm32逆向入門
Intelligence test to see idioms guess ancient poems wechat applet source code
随机推荐
Acrel-EMS综合能效平台在校园建设的意义
MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
[IELTS reading] Wang Xiwei reading P3 (heading)
Using the uniapp rich text editor
Build your own minecraft server with fast parsing
Observable time series data downsampling practice in Prometheus
Galera cluster of MariaDB - dual active and dual active installation settings
[Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
uniapp 除了数字,其他输入无效
【kotlin】第三天
Servlet+jdbc+mysql simple web exercise
MariaDB's Galera cluster application scenario -- multi master and multi active databases
What is the difference between port mapping and port forwarding
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
Redis: redis transactions
Face recognition 5- insight face padding code practice notes
如果炒股开华泰证券的户,在网上开户安全吗?
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
城市轨道交通站应急照明疏散指示系统设计
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录