Python yield and C++ friend/static Keywords

Coding · Easy · Free problem

This problem covers three keywords that come up constantly in systems and quant coding interviews.

(a) Explain the yield keyword in Python. What does it do, how does it differ from return, and why would you use it?

(b) Explain the friend keyword in C++. What does it grant, and what are the typical use cases?

(c) Explain the static keyword in C++. It has several distinct meanings depending on context -- cover at least three of them.

Hints

  1. For yield: think about what happens to memory if you use a list vs. generating values one at a time.
  2. For friend: it is not about inheritance -- it is about granting controlled access across the normal encapsulation boundary.
  3. For static: there are (at least) four distinct meanings -- static member variable, static member function, static local variable, and file-scope static. Cover each one separately.

Worked Solution

How to Think About It: These are language-mechanics questions. The interviewer is testing whether you actually write code in these languages or just know them superficially. For each keyword, lead with the core idea in one sentence, then give the key use case. Don't recite a definition -- explain *why* the feature exists.

---

Part (a): Python yield

yield turns a function into a generator -- instead of computing all results and returning them at once, execution is suspended at each yield and resumed on the next call to next().

```python def countdown(n): while n > 0: yield n n -= 1

for x in countdown(5): print(x) # prints 5, 4, 3, 2, 1 ```

Key properties: - The function returns a generator object when called. No code runs yet. - Each next() call runs until the next yield, then suspends. - Local state (variables, position) is preserved between calls. - Memory-efficient: you never hold all values in memory simultaneously.

When to use it: large data pipelines, infinite sequences, lazy evaluation. Classic examples: reading large files line by line, implementing coroutines, building itertools-style utilities.

---

Part (b): C++ friend

friend grants a non-member function or an external class access to the private and protected members of the class that declares the friendship.

```cpp class Matrix { double data[4][4]; public: friend Matrix operator*(const Matrix& a, const Matrix& b); // friend function friend class MatrixSerializer; // friend class }; ```

Key points: - Friendship is declared *inside* the class granting access -- the granting class is in control. - It is not symmetric (A friends B does not mean B friends A) and not transitive. - Common use cases: operator overloading (especially << for stream output), tightly coupled utility classes (serializers, test fixtures), factory patterns.

---

Part (c): C++ static

static has four distinct meanings depending on context:

1. Static member variable: shared across all instances of the class. Useful for counters, caches, constants. ```cpp class Foo { static int instance_count; // one copy, shared by all Foo objects }; ```

2. Static member function: can be called without an object instance. Cannot access non-static members (no this pointer). ```cpp Foo::reset_counter(); // no object needed ```

3. Static local variable: initialized once, persists across function calls. Classic use: Meyers singleton pattern. ```cpp Config& Config::get() { static Config instance; // created once, on first call return instance; } ```

  1. Static at file scope (in a .cpp file): limits the symbol's linkage to the current translation unit -- other .cpp files cannot see it. Prefer anonymous namespaces in modern C++, but static still works.

Answer: yield creates lazy generators in Python; friend grants private-member access to external functions/classes in C++; static in C++ means one of four things depending on context -- shared class state, no-instance method, persistent local variable, or translation-unit-local symbol.

Intuition

The yield keyword is one of Python's most useful features for production code. In quant work, you often process data streams that are too large to hold in memory -- market tick data, simulation paths, feature pipelines. Generators let you express these as clean, readable functions without materializing the full result. The mental model is: a generator is a resumable function. Every yield is a checkpoint.

The C++ static keyword is a classic interview gotcha because it is genuinely overloaded -- the same word means four different things in four different contexts. The common thread is persistence or restriction: static things either outlive their normal scope (local static), are shared rather than per-instance (member static), or are restricted to their file (file-scope static). Candidates who conflate these contexts or give only one meaning signal shallow C++ knowledge.

Open the full interactive solver →