Today I gave a C++ backend interview. Here’s how it went.
Tags: uncategorized, datastructures, interview, performance
Published: 2024-05-16 13:12:34

---
title: Today I gave a C++ backend interview. Here’s how it went.
published: true
date: 2024-05-16 12:57:45 UTC
tags: Uncategorized,DataStructures,interview,Performance
canonical_url: https://smrtdvlpr.wordpress.com/2024/05/16/today-i-gave-a-c-backend-interview-heres-how-it-went/
---

I was just told by the HR that this round will be C++ focused and will also focus on other technical concepts. So I prepared the following concepts:

- C++ Language
- Data Structures and Algorithms
- Performance
- Computer Network

I didn’t have much time to prepare, but I relied on what I had already learned and building on top of that foundation. Here’s how I went about my preparation:

1. **C++ Preparation:** 
  - **Medium Article:** I found this [amazing article on Medium](https://medium.com/@alexander.s.augenstein/how-i-passed-the-c-code-interview-in-3-weeks-a3e350214a01) which covered my weak topics. Interestingly, these weak topics are often the most frequently asked in interviews.
  - **Github Notes:** Another great resource I went through was this [notes published on github](https://encelo.github.io/notes.html)for C++. This covers all the essential concepts and also highlights good questions and tricky concepts.
  - **InterviewBit Questions:** To bolster my confidence, I reviewed the most commonly asked [C++ questions on InterviewBit.](https://www.interviewbit.com/cpp-interview-questions/) These questions served as great refreshers.
2. **Resume Preparation:**
  - **Past projects** : I made sure to revise and go through my past projects, since they can be asked at any time during the interview.
3. **Computer Network:**
  - For the Computer Networks section, I utilized resources from InterviewBit and GeeksforGeeks (GFG), focusing particularly on the topics I was less familiar with.

## The Interview

#### Question 1: TCP vs UDP

- **Question** : Explain the difference between TCP and UDP.
- **Answer** : TCP (Transmission Control Protocol) is connection-oriented, ensuring reliable and ordered delivery of data. UDP (User Datagram Protocol), on the other hand, is connectionless and does not guarantee delivery, making it faster but less reliable. Since TCP is lossless protocol it is slower than UDP. Example of TCP can be website data or Financial information received through FIX protocol, whereas UDP example can be a video call or media streaming.

#### Question 2: TCP vs UDP Usage

- **Question** : Where would you prefer to use TCP, and where UDP?
- **Answer** : TCP is preferred for applications requiring reliable communication, such as web browsing (HTTP/HTTPS) and email (SMTP). UDP is suitable for real-time applications like video streaming and online gaming, where speed is crucial and occasional data loss is acceptable.

#### Question 3: Finding Target Sum in Vector

- **Question** : Find a target sum in a vector.
- **Approach** : Initially, I used brute force, then optimised it with binary search.
- **Follow-up** : The interviewer asked why instead of using the traditional method of calculating:

```

mid = (low + high)/2;

```

I used:

```

mid = low + (high-low)/2;

```

I explained that using the below approach will avoid the case of overflow.He also asked if `x/2` is faster than `x >> 1`. I answered bitshifting will be faster than dividing by 2. Then he corrected me, C++ optimizes these operations, so it doesn’t matter which one is used. Therefore, C++ by default uses bitshifting to divide here by 2. So I need not substitute /2 with >>1.

#### Question 4: Pass by Reference vs Pass by Value

- **Question** : Explain Pass by Reference vs Pass by Value.
- **Answer** : Passing by reference avoids duplicating the data, improving performance. However, it risks unintended modifications to the original data.
- **Follow-up** : To prevent unintended changes, the interviewer suggested using the `const` keyword when passing arguments by reference.

#### Question 5: Copying uint8\_t Variables

- **Task** : Create four `uint8_t` variables with assigned integers and copy them into a `uint32_t` variable.
- **Approach** : I mentioned two options: using `memcpy` and using bitwise operations (left shifting and ORing).
- **Follow-up** : The interviewer instructed me to explore `memcpy` on my own. While attempting this, I encountered an error: “invalid conversion of type”. We discussed why `cout` doesn’t print `uint8_t` as an integer and the peculiarities of `char*` handling in `cout`.
- **Final Task** : Segregate the `uint32_t` back into four `uint8_t` variables.

## **Key Learnings**

The interview was a rigorous yet enlightening experience. It highlighted the importance of understanding both high-level concepts and low-level details in C++. Revising from various resources paid off, especially focusing on commonly asked questions and tricky concepts.