Analyze a question of Alibaba operation and maintenance P7 job interview

Analyze a question of Alibaba operation and maintenance P7 job interview

This year, my colleague was fortunate enough to recommend to Alibaba for an interview. The position was that the operation and maintenance of the traffic business department started smoothly on 3 sides and was highly evaluated. When the fourth side crossed, I was a P9 senior. Needless to say, the technology is directly crushed. My kind, I asked some methodological questions during the interview, but I didn’t answer very well. In the end, he asked me a shell question to ease the atmosphere. The first question is how to query duplicate records in the text. I answered this, and another question is to directly give me a string of strings; for example, str='ieuwugfuwgfugugfujdfjh' lets me count the character with the most occurrences of that character. When using the shell, I thought it was easy at that time. Isn’t it just row-to-column? , But when I started writing, I was blocked by awk, and sed could not be used at once. I thought it was not completed for a long time. After that, I realized it myself. I used 2 methods:

method one

Use shell string slicing to process this string:

[root@VM_39_7_centos ~]# str='ieuwugfuwgfugugfujdfjh'
[root@VM_39_7_centos ~]# for((i=0;i<${#str};i++));do echo ${str:$i:1};done|awk '{arr[$0]++} END{for (i in arr) print arr[i],i}'|sort -nr
6 u
4 g
4 f
2 w
2 j
1 i
1 h
1 e
1 d
[root@VM_39_7_centos ~]# for((i=0;i<${#str};i++));do echo ${str:$i:1};done|awk '{arr[$0]++} END{for (i in arr) print arr[i],i}'|sort -nr|head -1
6 u
[root@VM_39_7_centos ~]# 
Method Two

Use sed's regular expression to match characters and then row to column;

[root@VM_39_7_centos ~]# echo ${str}
ieuwugfuwgfugugfujdfjh
[root@VM_39_7_centos ~]# echo ${str}|sed  's/\([a-z]\{1\}\)/\1 \n/g'
i 
e 
u 
w 
u 
g 
f 
u 
w 
g 
f 
u 
g 
u 
g 
f 
u 
j 
d 
f 
j 
h 

[root@VM_39_7_centos ~]# echo ${str}|sed  's/\([a-z]\{1\}\)/\1 \n/g'|awk '{arr[$0]++} END{for (i in arr) print arr[i],i}'|sort -nr|head -1
6 u 
[root@VM_39_7_centos ~]# 
to sum up

It’s a pity that I didn’t work out it at that time. Maybe I was nervous at that time, but everything has its inevitability. My question should be whether the foundation is not solid enough. I will work harder in the future and pay attention to methods, standards, and work. ;

Guess you like

Origin blog.csdn.net/qq_31555951/article/details/106596967