Interview Basics| Clean coding

Sandeep Kumar
3 min readAug 14, 2022

--

Clean coding requires a lot of thought process

This blog talks about a “practical” way to keep your code clean during interviews. Most people make the common mistake of not taking it into consideration during practice.

The day you admire a piece of code just for the sheer beauty of it, irrespective of it’s functionality, you shall start understanding what clean code is.

Things to keep in mind

  1. In most cases, interviewers are experienced enough to understand that writing a production-level clean code requires one to put more thought than just 5–6 minutes.
  2. The interviewer is only human, don’t expect them to understand your approach in a second.
  3. During an interview, the aspects that are mainly in focus are naming and readability. Testing and scaling are secondary aspects here.
  4. Talk - The biggest mistake possible during an interview is writing 50 lines of code while being quiet.

Common Mistakes

  • Single-letter variables are bad in most cases. (x and y as coordinates are just fine). Ex: Using i and j as the iterator variables.
Clean Coding Mistakes
  • Long ass names Just because you want to keep things clear doesn’t mean your variables end up being 1 sentence long because that makes your code illegible.
Clean Coding
Alternative names to use by shortening existing things
  • Using too many expressions in an if-else statement. There is nothing wrong with the statement below, but moving it to a function as shown in the next image offers more clarity about what the code is doing.
Clean Coding Mistake
Clean Coding Mistakes — Using Multiple Checks in a simple If statement
Clean Coding Example
Clean Coding Example
  • Some companies choose not to allow IDEs during interviews (sed 😓) . This leads to random {} syntax and also typos while reusing variable. (These are usually overlooked during interviews, but should be taken care of when you recheck your code.)
  • Not using comments when you write code.

Tricks

  1. Keeping talking. Explaining to the interviewer while you code helps you get away with a lot of shitty names ;).
  2. If you don’t have a decent variable name, just call it out right then and there. Inform the interviewer that you will come up with it later. This will let you keep up your speed.
  3. Adding comments to offer more clarity. It can also be used to move ahead on complicated pieces of code that the interviewer would be struggling to understand.
  4. Use the variables that the interviewer provides. Ex: If he says that the number of students in a bus is represented in a list buses. Use the variable name as buses. Don’t go around with studentsInBus.
  5. Lastly practice. It is the ultimate trick.

Example Code snippet

Just providing the code here. Complete questions and discussions are available in the comments of the link.

Clean Coding Example
https://leetcode.com/discuss/interview-question/356477

You can go through this list of Google interview questions. Their discussions contain pretty great examples of clean codes.

Please let me know in the comments if you don’t understand anything. Also, let me know if you guys face any other issues that I should be writing about.

Follow me on Medium. 🤗
Hit me up on LinkedIn.
Hire me on Topmate.

--

--