Featured post

Don’t know why you’re failing Coding Interviews? It’s likely this ?

Picture this: You just had your first coding interview and you are confident you will pass to the next stage. You discussed different approaches, you found the optimal solution, you implemented it and described the Big O time complexity. And the verdict comes: You won’t pass to the next stage!

It happened to me before and it took me many failures to figure out what I was doing. It’s the way you write your code. Let’s see if you recognize mistakes you make on a daily basis:

  1. Bad variable and function names: If your map is called ‘mp’ and your returned result is called ‘arr’, you need to step it up!
  2. Not respecting a Coding Style. You don’t make use of enough spaces and tabs and your brackets are all over the place
  3. Your code is over-complicated, hard to follow. This is a sign that you jumped too quickly into coding without having a clear picture of how exactly it will work and how to divide it into smaller components. Things like putting everything in one function or just having too many obfuscated if statements that can be rewritten in a simpler and easy to follow way. This is the reason why interviewers ask you to walk through an example over your code.

Code Quality is the most important thing interviewers are looking for. Many times a non-optimal well written solution will pass but an optimal bad implementation will not.

Featured post

What to do when you freeze during a coding interview

Yes, this happens to everybody.

You have prepared hard for the final day in which you should be crushing it! Those 200+ questions solved lately are definitely going to pay off! And they do not! You receive a problem which has nothing to do with the ones you have solved before. Or at least that’s what you think! Your response resembles that popular Disney movie with the blonde princess: Frozen. Here’s what to do in these situations:

  1. How quick and seemingly comfortable you have solved the question is not the point. It is something like 10% of the whole process. Do you think the interviewers read the questions they are supposed to ask you and poof, they instantly come up with the best solution? No, they are also human and they take serious time to make sure they are prepared to guide you during the interview (or at least they should be).

  2. I know it seems impossible on the spot, but what you have to do in this type of scenario is to open your mouth. Nobody likes a person which can’t express their feelings. It’s the biggest red flag you can show a interviewer. That person not only wants to test your coding skills, but how good of a team player you are. The second one is most of the time more important than the first. History is the best of argument why. So, forget about EGO, solving the problem perfectly from the first try faster than Usain Bolt. Just talk. Say to the recruiter what ideas you have and why they don’t work here. Say that you don’t have any idea. Ask for a hint. You are not going to be the prophet that solved the problem with no effort, but you get extra points for communication.

  3. There are bad days. Don’t give up! The human brain and body are so complex that we still don’t possess fully understanding of. You can’t expect for all of your performances to be the exact same. If I think about it, you don’t want to! It would be too boring and predictable. Where is the challenge in that? You responsibility is to acknowledge this and yes, work hard to minimize the chances of happening. Solve your performance anxiety, social skills and pressure management. Do everything you need to do! But don’t expect the chance of having a bad day to hit 0% because it will never do. Have fun and at least solve the problem the next day, I often solve them in less than 5 minutes the next day.

Enjoy the hero’s journey,

Andy

Choosing the right programming language for your interview

You recently started coding in Python or JavaScript and you realize it’s a way better choice for your upcoming coding interview. But is it really? Is it the best choice for you?

You can easily fall into the trap of thinking you know how to code in a new language, but you might not realize some fine details that ends up being a disaster for your coding interview.

Let’s take the following problem: You are given a string and you need to return the reverse string without using builtin functions. You quickly jump in Python and write the following:

def reverse(input):
    result = ''
    for char in input:
        result = char + result
    return result

Simple and efficient, isn’t it? Or is it? Your interviewer asks what is the time complexity of this? And you quickly answer that it’s linear time complexity because you iterate through each character once.

You see, this is where you failed because you didn’t know that strings are immutable in Python. What does this mean? Well, it means that \(char + result\) does not run in constant time, but rather in \(O(n)\) because it needs to copy the two strings into another one. The final time complexity ending up being \(O(n^2)\).

Let’s take another example. You are given a problem where you need to sort an array of integers as an initial step. You quickly jump and write the following JavaScript code:

nums.sort()

This surely looks right, no? Well, let’s say \(nums \) is \([1, 2, 11]\). After sorting you will notice that you end up with \([1, 11, 2]\). Not exactly what you would expect, is it?

Well, by default JavaScript considers your objects as strings and sorts them in alphabetical order, so now it’s obvious that 11 is less than 2 in alphabetical order! To make it work you need to use a comparator function that specifies how to compare two values:

nums.sort((a, b) => a - b)

One last example, let’s say you have an array and you want to print pairs containing the index and the value, but the index needs to start at 1. So you start writing the following JavaScript code:

for (let i in nums) {
    let index = i + 1
    console.log(index, ': ', nums[i]) 
}

That should do the trick, right? Well, let’s see for \(nums=[1, 2, 3]\) we get
01: 1
11: 2
21: 3

What happened? Well, the \(in\) operator is not meant to be used for arrays, but for objects. While it will give you the indices 0, 1 and 2, they will be strings, so when you add 1, you will actually concatenate the two strings.

What I’m trying to say is that you should stick with the programming language you have deep knowledge about and feel comfortable writing code without the use of an IDE / autocomplete / syntax highlighting, possibly even on a whiteboard.

10 reasons why you keep failing coding interviews

Have you ever looked at the source code that people write in the comments on platforms like LeetCode? You will soon realize that only knowing how to write code that passes all test cases is not enough to get your dream job!
Here’s a list of things the interviewer is assessing you on:

  1. Can solve a problem at first sight and not just recite an algorithm by heart?
  2. Are you able to communicate well with the interviewer?
  3. Do you explain your ideas in a simple and clear way?
  4. Are you able to collaborate with your interviewer, by taking the lead, to solve the task at hand?
  5. Do you discuss pros and cons of different approaches such as time complexity and memory consumption?
  6. Can you design the major components of the algorithm instead of improvising while you write the code?
  7. Do you write clean and easy to understand code?
  8. Is your code extensible?
  9. Are you able to run your code on a simple test case?
  10. Can you find edge test cases?
Also, the most important thing the interviewer wants to figure out is:
Would I hang out for a beer with you after work? Do I see a potential colleague I can work well with?

How to apply memoization in Python using functools

Memoization is a technique used to optimize an exponential recursive function into a fast polynomial one.

Let’s take the classic problem of Fibonacci numbers. The recursive \(O(2^n)\) function would look like this:

def fib(n):
  if n < 2:
    return n
  return fib(n - 1) + fib(n - 2)

In order to obtain an \(O(n)\) time complexity, we want to transform it using memoization. Because \(fib(n)\) is called multiple times, we want to store the result in a cache the first time we compute it, and retrieve it from there on subsequent calls.

def fib(n, cache = {}):
  if n < 2:
    return n
  if n in cache:
    return cache[n]
  cache[n] = fib(n - 1, cache) + fib(n - 2, cache)
  return cache[n]

Functools offers a way to cache the results of a function, considering the parameters are hashable, in a similar way the functional programming languages do.

from functools import lru_cache

@lru_cache(None)
def fib(n):
    if n < 2:
        return n
    return fib(n - 1) + fib(n - 2)

We can notice that the fib function remained the same, the only thing we added is a decorator @lru_cache with None as parameter, meaning the cache size is unlimited

Switching from Competitive Programming to Coding Interviews

After a pretty successful career in Competitive Programming, I have found myself to suck at coding interviews. How was this even possible? The questions that I had to deal with in the Central European Olympiad were way tougher than these. Are coding interviews broken or am I?

Neither, I got comfortable.

Having solved all those tough questions, I was foolish enough to think Coding Interviews require the exact same set of fundamentals.

Can you imagine how well I explained my thinking process and how my code looked like?
Reading the variables in my code was like reading the alphabet: ‘a’, ‘b’, ‘c’, … you got the idea. But I haven’t back then! And it cost me 2 years of doing well in every interview, but getting a quick confusing rejection.

4 things that I did to make the most out of every problem:
– be relentless in solving it without help
– see the patterns
– find different solutions
– obsess over the code. Write it as simple as possible, but not simpler.

I know it’s hard at start, by your own. Along with a friend, we launched a platform that helps people learn algorithms and ace coding interviews using step by step interactive tutorials and video content. Give it a shot: https://algocademy.com/
#algorithms #tech #interviews #faang