The first session on “Cracking the Coding Interview” was all about learning what happens under the hood in the C language programming. The C language will be used as a programming language to write code in future sessions when doing Data Structure and Algorithms. The C language was chosen because everyone knows it and it is small and fast to learn, plus it provides more visibility into the hardware.
Everyone in the audience knew C language basics. We started building on top of that. Let's recap what we have learned.
Here is the
Written Test DiscussionWhat happens under the hoodWhen we write code then convert it to an executable and then execute it, there are multiple steps involved in that, each of these steps were discussed in great detail.
Write CodeSeparating the code between header and code files. Declaration v/s Definition.
Pre-processingThe compiler looks for pre-processing tokens (#) and performs the required operation.
CompilingToken generation generally happens twice in the C language (once during pre-processing and then during compiling)
LinkingStdio.h has the declaration of the printf function. The actual definition comes during the linking phase. Similarly if we just declare a function and don't define it, then it will fail during linking.
LoadingWhen a program is loaded into memory (allocated by the OS):
- The code (machine language instructions) goes in the code area.
- Global and Static variables are allocated memory in the data area.
- Activation records of functions go in the Stack Area
- Memory that we allocate using malloc, calloc, realloc goes in the heap.
Question
What is the error in the following code?
- int x = y;
- int main()
- {
- Int y = 5;
- }
ExecutingDiscussed the memory snapshot when we call functions and how it is for recursive functions.
Memory stack overflow v/s Infinite ExecutionQuestionWhy is the following code dangerous:
- int x = 0;
- int a() { x = 10; return 2; }
- int a() { x = 10; return 2; }
- int main()
- {
- int y = a() + b();
- }
Deep dive into pointersPointers are the biggest strength available to developers of the C language, it is also the biggest source of errors. There are just two operators:
- &: address of
- *: indirection (value at )
rValue v/s lValue: Probably the most important concept to understand. When a variable is defined it has both a rValue and lValue. Literals don't have a rValue. Learn more at Difference between lvalue and rvalue expressions
There are only two problems that can exist when programming using pointers:
- Memory Leaks
- Dangling pointers
Learn about then at Memory leaks and dangling pointers
Learn about Complexities
Whenever we write any code, we need to see how much time and how much memory our code is taking. There should be a standard terminology for expressing how your program performs (time taken or extra memory taken).
Why are algorithms always accessed in the terminology or "Worst Case complexity"?
How recursive functions take more memory and more time, but are still used because in some places it is easier to write code using recursion than using iteration.
What comes next
This session was just to bring everyone to the same page before getting into the actual Interview Preparation mode starting next session.
Audience Feedback
In the next session we will start with Searching problems. How to search in an Array, List, Tree, Graph and so on.
We will also talk about various sorting algorithms and compare the time and memory taken by various algorithms and where they are applicable.