To further confirm that you understand the problem, you can come up with an algorithm that throws runtime to the wind and assumes infinite time and resources. Several factors affect how I code in an interview, such as time and responsiveness of the interviewer. Either as a code comment or in the corner of the whiteboard. Regardless of whether you made the attempt (if you did, kudos to you for the awesome study ethic), here is the final optimized version. I also picked this problem as it is somewhat practical. While there is most likely a more space efficient solution, I can optimize my solution later. You might be able to get some points by making it clear that you can stick to first principles. If there’s already a procedure you use, practice and perfect it. Though you’re going to have to take my word for it. Some people would probably start coding after this step. For me, since I’ve mainly been doing Machine Learning, my languages of choice might be either Python or JavaScript. n=6n = 6n=6 The code to read the inputs from stdin and to pass it to the function connectedCities is provided for you. One issue I can already foresee is keeping track of the places of each digit (tens, hundreds, thousands, etc). . I know I can solve it and I have put myself in the best position to do so. Our mission: to help people learn to code for free. If you don’t have a process, start making one. But that’s not the point of this post. For our problem, the inputs and outputs are nicely defined in the title. If you jump into coding without a plan, you could end up confusing both yourself and your interviewer. While the former method may work for some, I exercise the latter. I was given this problem: You are given a sequence of n distinct integers a0, a1, . Sample Input 2 With that in mind, I decided to try and replicate the experience in blog-post form. Biologist-turned-ML-Engineer in San Francisco. I take algorithms and put them in a scene from everyday life, such as matching socks from a pile, putting books on a shelf, remembering things, driving from one point to another, or cutting an onion. Divide and Conquer is the general way to approach and to solve a complex problem. In this article, I want to show you the process I have honed through several technical screens and dozens of mock interviews. And practice you should. It will get easier with practice. While there’s an ever-expanding list of companies that don’t make you jump through coding hoops, the average developer will encounter a live algorithm challenge sometime in their job hunt. Indeed, I spend a lot of time in planning mood. Keeping my panic at a tolerable level helps me to focus. ! And to make this as authentic as possible, I’ll pick a problem I’ve never solved before. n=6n = 6n=6 Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Most often these examples are very generic (e.g, may just require memorizing how to do BFS, for example), or are almost insultingly simple (i.e., you probably don’t need the special workflow if you’re just doing fizzbuzz, but in real life you’re going to encounter problems much tougher than that). Get started, freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546). That’s the sort of restriction I would expect to find in an actual interview. Then interviewers caught on to the patterns and problems from that book. Let’s try another example output to this, perhaps one that explicitly does NOT result in similar outputs as the one before. For me, having a set of steps to use to break a problem down is crucial. It also has the lowest completion rate of any Medium ranking problem. Without access to Google or sufficient time to refactor, I just want to write something that works. We also have thousands of freeCodeCamp study groups around the world. However, it would pass most technical interviews. Especially if you want to work for a Big Four or an established startup. If you manage to do this on a whiteboard, you are either brilliant when it comes to low-level systems, or you are a masochist. Have 200K+ Americans really died from COVID? Then sites like Leetcode started coming up. Once you try another scenario like this, it should become easier to see a pattern. . Divide and conquer? These examples need to cover all the edge cases I can think of. Nobody likes interviewing interviewing a candidate for 3 stages of interviews, only to cut things off at the third.