Residency certainly seems like an elaborate hazing ritual. I believe there is something to be said for errors made when handing off patients between doctors at the end of a shift, which is why you would want to train physicians to maintain good performance even over long shifts. But in general, modern residency was invented by a morphine- and cocaine-addicted surgeon who decided, hey, I want you to undergo a grueling residency for an arbitrary number of years and you'll be a doctor when I say you are: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7828946/