Interview prep
The last video turns the course into interview answers: C# output puzzles you should be able to solve in seconds, the most-asked ASP.NET Core and EF Core questions with short answers, and a three-step formula for answering any question.
75.1 Puzzle 1: struct vs class copy
- Money is a struct, Wallet is a class; both start at 100 and a copy is set to 500.
- Many people say 500 for both; the real output is 100 and 500.
var m1 = new Money { Amount = 100 };var m2 = m1;m2.Amount = 500; var w1 = new Wallet { Balance = 100 };var w2 = w1;w2.Balance = 500; Console.WriteLine(m1.Amount);Console.WriteLine(w1.Balance); struct Money { public int Amount; }class Wallet { public int Balance; }100500- Step 1
1.var m2 = m1 (struct Money): the whole value is copied
- Step 2
2.m2.Amount = 500 changes only the copy; m1 stays 100
- Step 3
3.var w2 = w1 (class Wallet): only the reference is copied
- Step 4
4.w1 and w2 point to the same object on the heap
- Step 5
5.w2.Balance = 500 is visible through w1 -> 500
Value type: copy. Reference type: shared object (video 5)
75.2 Puzzle 2: async print order
- PayAsync() is called without await on the second line.
- An async method runs synchronously until the first await on an incomplete Task, then returns control to the caller.
- So B prints right after A, C prints while the delay runs, D after the delay, then E: A B C D E.
Console.WriteLine("A");Task task = PayAsync();Console.WriteLine("C");await task;Console.WriteLine("E"); async Task PayAsync(){ Console.WriteLine("B"); await Task.Delay(100); Console.WriteLine("D");}ABCDE- Step 1
1.Caller prints A and calls PayAsync()
- Step 2
2.PayAsync prints B synchronously
- Step 3
3.await Task.Delay(100) is incomplete -> control returns to caller
- Step 4
4.Caller prints C, then await task waits
- Step 5
5.Delay finishes: PayAsync prints D and completes
- Step 6
6.Caller resumes and prints E
75.3 Checkpoint: deferred execution
- Where only builds the query; it runs when Count() enumerates the list.
- By then the list has 100, 200, 300, 400, and three are above 150: output 3 (video 32).
var prices = new List<int> { 100, 200 };var costly = prices.Where(p => p > 150); prices.Add(300);prices.Add(400); Console.WriteLine(costly.Count());375.4 ASP.NET Core: most-asked
| Question | Short answer |
|---|---|
| DI lifetimes | Transient: new object every time. Scoped: one per request. Singleton: one for the whole app. Injecting scoped into singleton = captive dependency bug. |
| Middleware vs filters | Middleware runs in the pipeline on every request; filters run around the action inside MVC and see action arguments and result. |
| JWT vs cookies | HttpOnly cookie for browser apps; JWT bearer token for mobile or service clients. |
| Minimal APIs vs controllers | Both fully supported; Minimal APIs suit small focused APIs, controllers are common in large structured projects. |
75.5 EF Core: IEnumerable vs IQueryable
- With IEnumerable<Order>, Where runs in memory: the SQL has no WHERE and the whole Orders table loads.
- With IQueryable<Order>, Where builds an expression tree that EF Core translates into SQL WHERE.
- Same result, but on a table with lakhs of rows the first version slows the app down.
// Wrong: Whole table in memoryIEnumerable<Order> orders = db.Orders;var big = orders .Where(o => o.Total > 1000) .ToList(); // SQL (EF Core log):// SELECT "o"."Id", "o"."Total"// FROM "Orders" AS "o"// Right: Filter runs in the databaseIQueryable<Order> orders = db.Orders;var big = orders .Where(o => o.Total > 1000) .ToList(); // SQL (EF Core log):// SELECT "o"."Id", "o"."Total"// FROM "Orders" AS "o"// WHERE "o"."Total" > 1000| Point | IEnumerable<T> | IQueryable<T> |
|---|---|---|
| Where runs | In memory (LINQ to Objects) | In the database (translated to SQL) |
| Filter takes | Func<T,bool> delegate | Expression tree |
| Data loaded | Whole table | Only matching rows |
75.6 EF Core: most-asked
- N+1: one query loads a list, then one query per row. Fix with Include or a Select projection.
- Detect it with SQL logging or ToQueryString().
- Tracking: DbContext tracks loaded entities so SaveChanges can detect changes; use AsNoTracking for read-only queries.
- Concurrency: mention an optimistic concurrency token (like Stock in the capstone).
75.7 How to answer: the 3-step formula
- Definition in one line: IQueryable keeps the query as an expression tree that the provider translates into SQL.
- Example or code: the orders filter runs in the database's WHERE.
- Gotcha or trade-off: a filter after an IEnumerable variable runs in memory.
- Step 1
1.Hear the question
- Step 2
2.One-line definition
- Step 3
3.Small example or code
- Step 4
4.Gotcha or trade-off
- Step 5
5.Short answer that shows depth and survives the follow-up
75.8 Common mistakes
- Treating the interview as memorising syntax and definitions: interviewers check trade-offs (scoped vs singleton, Dapper vs EF Core).
- Memorised answers get caught on the first follow-up question.
- Bluffing: if you don't know, say so and explain how you would approach it; that is a strong answer too.
- Filtering after assigning a DbSet to an IEnumerable variable.
75.9 Preparation best practices
- Revise the interview scene of every video in the course: that is your revision list.
- Run small programs like these puzzles with dotnet run, and guess the output first.
- Put the capstone on GitHub and add the link to your resume.
- Do a mock interview with a friend and explain things out loud.
75.10 Interview tips
- Struct copy copies the value, class copy copies the reference.
- An async method runs synchronously until the first await; LINQ Where runs only when enumerated.
- Keep short answers ready for DI lifetimes, middleware vs filters, JWT vs cookies.
- In EF Core remember IQueryable, Include and AsNoTracking.
- Answer every question as definition, example, gotcha.