Skip to content

Small, interview-focused Java and Python code snippets that demonstrate core computer science concepts, common pitfalls, and frequently asked patterns.

Notifications You must be signed in to change notification settings

CynthiyaJohn/cs-interview-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 

Repository files navigation

Perfect. Your README is already strong — now I’ll edit and extend it so it EXACTLY matches your current file structure and adds the missing snippets cleanly, without bloating or changing your tone.

Below is the UPDATED, DROP-IN REPLACEMENT README.md 👉 You can replace your entire README with this.


CS Interview Snippets 🧩

A curated collection of small, focused Java and Python code snippets designed for Computer Science & Software Engineering interview preparation.

Instead of large projects, this repository emphasizes clarity, correctness, and conceptual understanding through minimal yet meaningful implementations.


📌 Why This Repository?

In technical interviews, candidates are evaluated on:

  • Strong Computer Science fundamentals
  • Ability to explain program behavior, not just syntax
  • Awareness of common pitfalls, design patterns, and concurrency issues
  • Understanding of language internals and memory models

This repository serves as a personal interview reference, containing frequently asked concepts implemented as clean, runnable, and explainable code snippets.


🧠 Key Philosophy

Each snippet in this repository:

  • Demonstrates one interview-relevant concept
  • Uses minimal and readable code
  • Avoids frameworks and unnecessary boilerplate
  • Can be explained verbally within minutes

One concept → One file → Clear explanation


🗂️ Repository Structure

cs-interview-snippets/
│
├── java/
│   ├── DeadlockVsStarvation.java
│   ├── RaceConditionDemo.java
│   ├── SingletonExample.java
│   ├── DoubleCheckedLockingSingleton.java
│   ├── ExecutorServiceDemo.java
│   ├── ImmutableClassDemo.java
│   ├── ThreadSafetyBasics.java
│   └── VolatileDemo.java
│
├── python/
│   ├── default_mutable_arguments.py
│   ├── gil_demo.py
│   ├── mutable_vs_immutable.py
│   ├── shallow_vs_deep_copy.py
│   ├── generator_vs_list.py
│   ├── decorator_basics.py
│   ├── context_manager_demo.py
│   └── list_vs_tuple_performance.py
│
└── README.md

☕ Java Snippets

1️⃣ Singleton Pattern (Basic)

File: java/SingletonExample.java

Demonstrates:

  • Lazy initialization
  • Controlled object creation
  • Ensuring a single instance across the application

📌 Interview angle: Why restrict object creation? How does Singleton differ from static classes?


2️⃣ Double-Checked Locking Singleton

File: java/DoubleCheckedLockingSingleton.java

Demonstrates:

  • Thread-safe lazy initialization
  • Use of volatile
  • Reducing synchronization overhead

📌 Interview angle: Why is volatile mandatory here? What problem does double-checked locking solve?


3️⃣ Deadlock vs Starvation

File: java/DeadlockVsStarvation.java

Demonstrates:

  • Circular lock dependency
  • Threads waiting indefinitely
  • Difference between deadlock and starvation

📌 Interview angle: How do deadlocks occur, and how can they be prevented?


4️⃣ Race Condition

File: java/RaceConditionDemo.java

Demonstrates:

  • Multiple threads accessing shared data
  • Inconsistent results due to lack of synchronization

📌 Interview angle: What is a race condition, and how does synchronization solve it?


5️⃣ Thread Safety Basics

File: java/ThreadSafetyBasics.java

Demonstrates:

  • Thread-safe vs non-thread-safe behavior
  • Basic synchronization mechanisms

📌 Interview angle: What makes code thread-safe, and why is thread safety important?


6️⃣ ExecutorService (Thread Pool)

File: java/ExecutorServiceDemo.java

Demonstrates:

  • Thread pooling
  • Controlled task execution
  • Proper shutdown of executors

📌 Interview angle: Why is ExecutorService preferred over manual thread creation?


7️⃣ Immutable Class Design

File: java/ImmutableClassDemo.java

Demonstrates:

  • Creating immutable objects
  • Defensive copying
  • Benefits of immutability in concurrency

📌 Interview angle: Why are immutable objects inherently thread-safe?


8️⃣ Volatile Keyword (Memory Visibility)

File: java/VolatileDemo.java

Demonstrates:

  • Java Memory Model visibility guarantees
  • Difference between visibility and atomicity

📌 Interview angle: Why does volatile not guarantee thread safety? When should it be used?


🐍 Python Snippets

1️⃣ Mutable vs Immutable Objects

File: python/mutable_vs_immutable.py

Demonstrates:

  • Behavior of mutable objects (lists)
  • Behavior of immutable objects (integers, strings)

📌 Interview angle: Why do changes to lists persist outside functions but integers do not?


2️⃣ Shallow vs Deep Copy

File: python/shallow_vs_deep_copy.py

Demonstrates:

  • Reference sharing in shallow copies
  • Fully independent objects in deep copies

📌 Interview angle: Why does modifying a nested object affect a shallow copy?


3️⃣ Default Mutable Arguments

File: python/default_mutable_arguments.py

Demonstrates:

  • Persistence of default mutable arguments across calls
  • Why this is a common Python pitfall

📌 Interview angle: Why should mutable objects not be used as default arguments?


4️⃣ Global Interpreter Lock (GIL)

File: python/gil_demo.py

Demonstrates:

  • Python’s Global Interpreter Lock
  • Limitations of CPU-bound multithreading

📌 Interview angle: What is the GIL, and how does it affect Python multithreading?


5️⃣ Generator vs List

File: python/generator_vs_list.py

Demonstrates:

  • Lazy vs eager evaluation
  • Memory efficiency of generators

📌 Interview angle: Why are generators more memory-efficient than lists?


6️⃣ Decorators (Function Wrapping)

File: python/decorator_basics.py

Demonstrates:

  • Function wrapping
  • Use of decorators for logging and monitoring

📌 Interview angle: How do decorators work internally?


7️⃣ Context Managers

File: python/context_manager_demo.py

Demonstrates:

  • Resource acquisition and release
  • with statement mechanics

📌 Interview angle: How does Python ensure resources are cleaned up properly?


8️⃣ List vs Tuple Performance

File: python/list_vs_tuple_performance.py

Demonstrates:

  • Memory footprint comparison
  • Mutability vs performance trade-offs

📌 Interview angle: Why are tuples more memory-efficient than lists?


🧠 Learning Outcomes

Through this repository, I aim to:

  • Strengthen core CS fundamentals
  • Improve interview explanations using real code behavior
  • Understand concurrency, memory visibility, and language internals
  • Build consistency through small, high-quality commits

🚀 Future Enhancements

Planned additions:

  • Producer–Consumer problem (Java)
  • Java happens-before relationships
  • Python multiprocessing vs threading
  • JVM vs Python memory model comparison

All future additions will strictly follow the one-concept-per-file rule.


⚠️ Note

This repository is intentionally minimal and focused. It is meant for learning, revision, and interview preparationnot for building production-grade applications.

About

Small, interview-focused Java and Python code snippets that demonstrate core computer science concepts, common pitfalls, and frequently asked patterns.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published