Overview
The imperial examinations (科举, Keju) were the civil service examination system of imperial China. They are central to Zhong Kui's origin story: he aced the examinations, earned the title of Zhuangyuan (top scholar), and was stripped of his honors because of his appearance. The injustice of that decision (his intellect recognized but his person rejected) drove him to suicide and set his mythology in motion.
The system under the Tang dynasty
Under the Tang dynasty (618-907 CE), six categories of regular examinations were organized by the Ministry of Rites: cultivated talents (秀才), classicists (明经), presented scholars (进士), legal experts, writing experts, and arithmetic experts. In 622 CE, the examinations were opened to most men. Emperor Xuanzong (the emperor in Zhong Kui's legend) added categories for Daoism and apprentices during his reign.

The jinshi degree
The jinshi (进士, "presented scholar") was the hardest and most prestigious category. Between 730-740 CE, a section requiring the composition of original poetry (both shi and fu forms) was added, making it even more demanding. The success rate for other examinations was 10-20%, but for the jinshi it was only 1-2%. About 1,000 candidates sat for the jinshi each year, and a total of only 6,585 jinshi were created during the entire Tang dynasty, an average of about 23 per year.
The Zhuangyuan title
Zhuangyuan (状元, literally "top thesis author") was the jinshi who ranked first overall in the national examination. The top three were: Zhuangyuan (1st), Bangyan (榜眼, 2nd), and Tanhua (探花, 3rd). By Xuanzong's reign, about a third of Grand Chancellors were jinshi degree holders. The title carried enormous prestige. These were the most brilliant minds in the empire, selected through the most rigorous meritocratic system the pre-modern world had devised.
Zhong Kui's story in context
Zhong Kui earning the Zhuangyuan title and then being stripped of it because of his face was not just a personal insult. It was a systemic failure. The entire point of the examination system was to select officials based on merit, bypassing the aristocratic nepotism of earlier dynasties. For the emperor to override the exam results based on appearance was a betrayal of the system's founding principle.
This is what gives Zhong Kui's story its moral weight. He did everything right. He was the best. And the system that was supposed to reward merit failed him because of superficial prejudice. His rage is justified. His suicide is an act of protest. And his appointment as King of Ghosts by Yanluo Wang (who, unlike the mortal emperor, judged him on his abilities) is the mythological correction of a worldly injustice.