"Word Frequency" is a unique challenge typically solved in a Unix Shell environment. You are given a text file and asked to count the frequency of each word and output the results sorted by frequency in descending order.
This is a popular question for DevOps, SRE, and Backend roles at companies like Google and Bloomberg. It tests your familiarity with command-line tools and the ability to pipe multiple small utilities together to solve a data processing task. It evaluates how you handle text formatting, sorting, and counting without writing a full-blown program in a language like Python or Java.
The pattern involves a chain of shell commands:
cat to read the file.tr or sed to replace spaces/newlines and normalize the text.sort to group identical words together.uniq -c to count the occurrences of each grouped word.sort -nr to sort the counts numerically in descending order.awk to format the final output.File content:
sky is blue
blue is nice
sky, is, blue, blue, is, nice.blue, blue, is, is, nice, sky.2 blue, 2 is, 1 nice, 1 sky.2 is, 2 blue, 1 sky, 1 nice. (Note: Tie-breaking rules may apply).sort and uniq wrong. uniq only works on adjacent duplicate lines, so you must sort before counting.Practice your basic Unix commands: grep, sed, awk, sort, and uniq. Being able to perform complex text processing in a single line of shell script is a highly valued skill in production environments.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Transpose File | Medium | Solve | |
| Tenth Line | Easy | Solve | |
| Valid Phone Numbers | Easy | Solve | |
| Find Unique Binary String | Medium | Solve | |
| Shifting Letters | Medium | Solve |