Man, that totally sucks. First off, be happy you didn't get that job. In my experience, the on-boarding process is usually when companies are the nicest they are going to be. If they were treating you like this before you were even on the payroll, just imagine what they would have been like once they felt like they owned you.
Second, it is my distinct opinion that the folks interviewing you were probably not actual developers themselves. They may have been development managers, Project Managers, or Product Managers, but I really doubt they actually dug into that much code themselves. From what I have found through the years, stating "That is incorrect"/"that is not the best way" means that, even if they are experienced developers, they are pretty closed minded and assume that they have the 100% correct answer to their question that is relevant to 100% of use cases, which is never true of any one person ever. Rather, they should have asked you why you chose the solution you did, what you felt made it the best solution, etc. Working successfully in a technology environment is about collaboration of people with different experiences, points of view, and methods of going about things. Simply saying that you are "wrong" without any feedback as to why or giving you any indication of your answer having any merit shows that they would likely be EXTREMELY resistant to doing anything differently, even if a better way were found at some point.
In my case, I had a job interview at one point. Phone interviews, remote coding challenges, algorithm tests, etc. I went in for an interview in-person for an engineer's position at this small company doing innovative work in ML and AI for shipping companies. The interview went great, the PM and Lead Engineer really seemed to hit it off with me. We had similar backgrounds, even personal interests. At the end of the interview, not only did they give me an offer with the salary they wanted to pay me, confirmed which of their benefits packages I wanted, they even gave me a start date. The next day, they called and asked for some MORE code samples, which I sent promptly. Repeatedly, I inquired as to when I would be receiving the job offer to sign so that I could get my ID badge made and repeatedly, they told me that they were just drawing it up and would send it by the end of the day. After two weeks of this weird stalling, I called and was a little more assertive about where the heck my letter was. They apologized, and I received a letter from them the next day. In summary, the letter said "Sorry if we mislead you and made you think like we had offered you the job, but we actually didn't. We have been considering other candidates and have selected somebody else. We apologize for any miscommunication."
Suffice to say, employers of all shapes and sizes all over the world pull this kind of garbage. Whether your looking to be a Junior Dev or Senior Engineer people will jerk you around. For some reason, companies are against simply saying "no thanks. You just wouldn't be a good fit for out team right now." Jerking you around seems to be part of process. Why it can't just be up-front, tactful (or at least civil), and quick so that we can move on with our job search and lives, I'm not sure. Either way, I feel your pain and hope you can find a company that will appreciate your skillset and viewpoint.
I don't know what country you are from, but know that its a problem all over the world. The "idiot-shaming" and "genius-developer" complex that most companies want is a huge problem everywhere. I have never tried to work in silicon valley, but even in the small tech area that I live in, the superiority complex that any company develops once it hits "unicorn" status is irritating. Image is huge in tech, and everything really. The more "relevant" a company thinks it is, the more arrogant and frustrating it is to interview with them. Professional Sports has figured out that paying a huge sum for one amazing player, then giving peanuts to a bunch of bums is not a recipe for success as it is a team effort to win consistently. Software is exactly the same. The idea of the 10x developer is a bit sad and unrealistic, creates dependence on one person, and limits innovation in a huge way. Keep trucking my friend. You'll get there!
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.