# Day 7: Tackling Mic Gain Problems and Exploring High-Frequency Trading

 Let’s be real: some days of development are 10% coding and 90% fighting your tools. Day 7 was exactly that—a mix of OBS audio tuning, "Day 1" algorithm logic, and deep-level C++ concurrency.

Check the live session here: **[Watch on YouTube](https://youtu.be/BjZvN737Qzw?si=kiar_-MKD4yecoxp)**

---

## 🎙️ The "Pre-Game" Struggle: Audio is Hard

Before hitting a single line of C++, I spent the first 10 minutes fighting **OBS Studio**. If your mic gain is too low, nobody listens. If it’s too high, you’re clipping and blowing out eardrums.

* **The Fix:** Added a Gain Filter in OBS, settling between **13 dB and 19 dB**.
* **Pro-tip:** Use Noise Suppression (RNNoise) if you’re coding in a room with a loud fan or Indian street traffic.

---

## 🧠 Algorithmic Evolution: The Sliding Window

We tackled the **Maximum Average Subarray** problem. The naive way to find the average of  elements in an array of size  is . That's garbage.

### The "Sliding Window" Optimization

Instead of re-summing  elements every time, you just:

1. **Subtract** the element leaving the window.
2. **Add** the element entering the window.

**Performance Hack:** Don’t calculate the average inside the loop. Division is expensive for a CPU. Compare the **sums** instead, and do a single division at the very end.

```cpp
// Optimized Logic
for (int i = k; i < nums.size(); ++i) {
    currentSum += nums[i] - nums[i - k];
    maxSum = std::max(maxSum, currentSum);
}
return (double)maxSum / k; // Single division = Win

```

---

## 🏎️ C++ Micro-Optimizations for HFT

When building a **Trading Terminal** (which I’ve renamed to **Hir Trader** to avoid trademark drama), every clock cycle is a liability.

### 1. Prefix vs. Postfix

Stop using `i++`. Use `++i`.

* `i++` (Postfix) creates a temporary copy of the object before incrementing.
* `++i` (Prefix) increments in place. In a loop running millions of times, those temporary objects add up to a performance tax you don't want to pay.

### 2. False Sharing & Cache Alignment

In multithreaded trading engines, two threads can fight over the same **Cache Line** even if they are touching different variables.

* **The Solution:** Use `alignas(64)` to ensure your atomic variables stay on their own cache lines. This prevents "False Sharing," which can destroy your throughput faster than a bad algorithm.

---

## 📅 The Workflow: Discipline > Motivation

I’m keeping a strict work log and using **WinGet** for package management to keep the environment clean. Most of the heavy lifting is in **C++ and CMake**, with a focus on benchmarking the matching engine to handle 100+ orders with sub-millisecond latency.

### Cultural Note

While the West worries about Friday the 13th, we’re out here celebrating **Sankranti and Ekadashi**. It's a good reminder that perspective changes everything—whether in life or in code.

---

### What's Next?

The benchmark results for the matching engine are coming next. I’ll be pushing the concurrency-safe structures to GitHub soon.

#CPlusPlus #HFT #CodingLife #SoftwareEngineering #Optimization #SlidingWindow
