ู‡ู†ุชุนู„ู… ู…ุน ุจุนุถ ุฅูŠู‡ ุงู„ู…ุณุงุฆู„ ุงู„ู„ูŠ ุจุชูŠุฌูŠ ููŠ ุงู„ู€ InterviewsุŒ ุฅุฒุงูŠ ู†ููƒุฑ ููŠู‡ุง ูˆู†ุญู„ู‡ุงุŒ ูˆุฅุฒุงูŠ ู†ุนู…ู„ Analysis ู„ู„ู€ Complexity

ุฃุบู„ุจ ุงู„ูˆู‚ุช ู‡ูƒูˆู† ุจุชูƒู„ู… ู…ู† ูˆุฌู‡ุฉ ู†ุธุฑ ุงู„ู€ Concepts ูˆุงู„ู€ Algorithms ูˆู…ุด ู‡ู‡ุชู… ุฃูˆูŠ ุจุงู„ู€ Implementation. ู†ุงุฏุฑุงู‹ ู„ู…ุง ู‡ุชู„ุงู‚ูˆู†ูŠ ุญุงุทุท ูƒูˆุฏ ูˆุจู†ุชู†ุงู‚ุด ููŠู‡ุŒ ุจุณ ู„ู…ุง ุฃุนู…ู„ ูƒุฏู‡ ู‡ุณุชุฎุฏู… C++.

ุฏูˆุฑูƒ ู…ู† ุงู„ู„ุญุธุฉ ุฏูŠ ุจูŠุชู„ุฎุต ููŠ ุญุงุฌุชูŠู† ู…ุงู„ู‡ู…ุด ุชุงู„ุช:

  1. ุชุงุฎุฏ ุงู„ู„ูŠ ู‡ู†ุดุฑุญู‡ (ุฒูŠ ุงู„ู€ BFS ุฃูˆ ุงู„ู€ DFS ุฃูˆ ุงู„ู€ Graph Traversal) ูˆุชุฑูˆุญ ุชุดูˆู ุงู„ู€ Implementation ุจุชุงุนู‡ ูˆุชุฐุงูƒุฑู‡. ุชุญุงูˆู„ ุชุนู…ู„ู‡ ุจู†ูุณูƒ (ูˆุบุงู„ุจุงู‹ ู‡ุชูุดู„ ููŠ ุงู„ุฃูˆู„)ุŒ ูˆุจุนุฏูŠู† ุชุดูˆูู‡ ูˆุชุฐุงูƒุฑู‡.
  2. ุงู„ุญู„ ุซู… ุงู„ุญู„ ุซู… ุงู„ุญู„! ุฃู†ุง ุจุดูู‚ ุนู„ู‰ ุงู„ู†ุงุณ ุงู„ู„ูŠ ุจุชุณุงูุฑ ู…ู† ูƒูุฑ ุงู„ุดูŠุฎ ูˆุบูŠุฑู‡ุง ุนุดุงู† ุชุญุถุฑ ุงู„ู€ 3 ุณุงุนุงุชุŒ ู„ุฃู† ูƒู„ ุงู„ู„ูŠ ุจู†ุนู…ู„ู‡ ุฏู‡ ุจูŠู…ุซู„ 20-30% ุจุณ ู…ู† ุงู„ู…ุฌู‡ูˆุฏ ุงู„ู…ุทู„ูˆุจ ุนุดุงู† ุชุนุฏูŠ ู…ู† ุงู„ู€ Interviews. ุงู„ู€ 70-80% ุงู„ุจุงู‚ูŠูŠู† ู‚ุงูŠู…ูŠู† ุนู„ู‰ ุฅู†ูƒ ุชุญู„ ูƒู„ ูŠูˆู…. ู„ูˆ ุญู„ูŠุช ุดู‡ุฑูŠู† ูˆุจุนุฏูŠู† ู‚ุทุนุช 6 ุดู‡ูˆุฑุŒ ูƒุฃู†ูƒ ู…ุนู…ู„ุชุด ุญุงุฌุฉ.

Course Content Overview

ุงู„ูƒูˆุฑุณ ุจุชุงุนู†ุง:

  1. ุงู„ู†ู‡ุงุฑุฏุฉ: Introduction ู„ู„ู€ C++ุŒ ุงู„ู€ STLุŒ ุงู„ู€ RecursionุŒ ุงู„ู€ Brute ForceุŒ ูˆุดูˆูŠุฉ Problem Solving Basics.
  2. ุงู„ุณูŠุดู† ุงู„ุฌุงูŠุฉ: ุงู„ู€ Complexity.
  3. ุงู„ุณูŠุดู† ุงู„ุชุงู„ุชุฉ: ุงู„ู€ Sorting ูˆุงู„ู€ Hashing.
  4. ุงู„ุณูŠุดู† ุงู„ุฑุงุจุนุฉ: ุงู„ู€ Trees.
  5. ุงู„ุณูŠุดู† ุงู„ุฎุงู…ุณุฉ: ุงู„ู€ Graphs.
  6. ุงู„ุณูŠุดู† ุงู„ุณุงุฏุณุฉ: ุงู„ู€ Dynamic Programming.
  7. ุงู„ุณูŠุดู† ุงู„ุณุงุจุนุฉ: ุงู„ู€ Problem Solving Techniques (ุฒูŠ ุงู„ู€ Sliding Window ูˆุงู„ู€ Two Pointers).

C++ Basics & Data Types

ู‡ู†ุจุฏุง ู…ู† ุงู„ุฃูˆู„ ุฎุงู„ุต ุจุงู„ู€ Data Types ุงู„ุฃุณุงุณูŠุฉุŒ ูˆุงู„ู„ูŠ ู‡ู…ุง: int, float, double, long, short, char, bool, void.

Pointer

ุชุฎูŠู„ ุฅู† ุงู„ู€ Memory ุฏูŠ ุนุจุงุฑุฉ ุนู† Grid ุงูˆ ุฏูˆู„ุงุจุŒ ูˆูƒู„ ู…ูƒุงู† ุจู†ุฎุฒู† ููŠู‡ ุจู†ุณู…ูŠู‡ Cell (ุฃูˆ ู…ุฑุจุน ุตุบูŠุฑ) ุงูˆ ุฑู ููŠ ุงู„ุฏูˆู„ุงุจุŒ ูˆูƒู„ Cell ู„ูŠู‡ุง ุญุฌู… (ู†ูุชุฑุถ ุฅู†ู‡ุง 1 Byte). ุนุดุงู† ุชุฎุฒู† ุญุงุฌุฉ ูƒู€ CompilerุŒ ู…ุญุชุงุฌ ุชุนุฑู ุงู„ู€ Address ุจุชุงุน ุงู„ู…ูƒุงู† (ุฑู‚ู… ุงู„ู€ Cell) ูˆุงู„ู€ Data ุงู„ู„ูŠ ู‡ุชุชุฎุฒู† ููŠู‡ุŒ ูˆุจุนุฏูŠู† ุจุดูˆูŠุฉ Assembly Code ุจูŠุชุนู…ู„ Save/Load ู„ู„ู€ Data ุฏูŠ.

ุงู„ู€ Pointer ููŠ C++ ู‡ูˆ Variable ุนุงุฏูŠ ุฌุฏุงู‹ุŒ ุจุณ ุงู„ู€ Type ุจุชุงุนู‡ ุฅู†ู‡ ุจูŠุดูŠู„ ุงู„ู€ Address ุจุชุงุน ุงู„ู…ูŠู…ูˆุฑูŠ ุฏูŠ. ู„ูˆ ุนู†ุฏู†ุง:

// Initialize x with 0
int x = 0; 

ุงู„ู€ Variable ุฏู‡ ุงู„ู€ Type ุจุชุงุนู‡ int ูˆุงู„ู‚ูŠู…ุฉ ุฒูŠุฑูˆ. ู„ูˆ ุนุงูŠุฒ ุฃุฌูŠุจ ุงู„ู€ Address ุจุชุงุน ุงู„ู€ x ุจุณุชุฎุฏู… ุนู„ุงู…ุฉ ุงู„ู€ Alias &x. ุงู„ู‚ูŠู…ุฉ ุงู„ู„ูŠ ุจุชุทู„ุน ุจุชูƒูˆู† ุฑู‚ู… Hexadecimal (ุฒูŠ 0x001...) ูˆุฏู‡ ุฑู‚ู… ุงู„ู€ Cell.

ู…ูŠู†ูุนุด ุฃูƒุชุจ:

// This will cause an error
int pointer = &x; 

ู„ุฃู† ุงู„ู€ Address ู†ูˆุนู‡ ู…ุด IntegerุŒ ู†ูˆุนู‡ โ€œPointer to Integerโ€. ุนุดุงู† ูƒุฏู‡ ุจู†ุนุฑูู‡ ุจุงู„ุดูƒู„ ุฏู‡:

// Pointer to integer
int* pointer = &x; 

ู‡ู„ ู‡ูˆ ุจูŠุงุฎุฏ ุฃูˆู„ Address ูˆู„ุง ุขุฎุฑ AddressุŸ ุงู„ุฅุฌุงุจุฉ ุฅู† ุงู„ู€ Integer ู…ุซู„ุงู‹ ุจูŠุชุฎุฒู† ููŠ ูƒุฐุง Cell (ุจู†ุงุกู‹ ุนู„ู‰ ุงู„ุณุงูŠุฒ ุจุชุงุนู‡ ู„ูˆ 4 Bytes ุฃูˆ ู„ูˆ long long ุจูŠุงุฎุฏ ู…ุณุงุญุฉ ุฃูƒุจุฑ)ุŒ ุงู„ู€ Compiler ู‡ูˆ ุงู„ู„ูŠ ุจูŠุนุฑู ูŠู€ Resolve ุงู„ู€ Cells ุฏูŠ ุจุงู„ุธุจุท ุจุงุณุชุฎุฏุงู… ุงู„ู…ูŠุชุง ุฏุงุชุง. ูˆุจุงู„ู…ู†ุงุณุจุฉุŒ ูƒู„ู…ุฉ new ุจู†ุณุชุฎุฏู…ู‡ุง ุนุดุงู† ู†ุญุฌุฒ ู…ูƒุงู† ููŠ ุงู„ู€ HeapุŒ ูˆู‡ู†ุชูƒู„ู… ุนู† ุงู„ู€ Heap ูˆุงู„ู€ Stack ูƒู…ุงู† ุดูˆูŠุฉ.

Array

ุงู„ู€ Arrays ููŠ C++ ุนุจุงุฑุฉ ุนู† Sequence of Data. ู„ูˆ ุนุฑูุช Array ูƒุฏู‡:

// Dynamically allocate array
int* array = new int[size]; 

ุงู„ู€ Array ู…ู„ูˆุด Data Type ู…ุฎุตูˆุต ููŠ C++ุŒ ู‡ูˆ ููŠ ุงู„ุญู‚ูŠู‚ุฉ Pointer to Integer ุจูŠุดุงูˆุฑ ุนู„ู‰ ุฃูˆู„ ู…ูƒุงู† (ุฃูˆู„ Element) ููŠ ุงู„ู€ Array. ูˆุจู…ุง ุฅู† ุงู„ู€ Elements ุจุชุงุนุฉ ุงู„ู€ Array ุจุชูƒูˆู† Contiguous (ูˆุฑุง ุจุนุถ ููŠ ุงู„ู…ูŠู…ูˆุฑูŠ)ุŒ ูุฅุญู†ุง ู…ุด ู…ุญุชุงุฌูŠู† ู†ุญุชูุธ ุจุนู†ุงูˆูŠู† ุงู„ู€ 10 ุฃูˆ ุงู„ู€ 100 Element ูƒู„ู‡ู…ุŒ ูƒูุงูŠุฉ ู†ุนุฑู ุนู†ูˆุงู† ุฃูˆู„ ูˆุงุญุฏุŒ ูˆู„ูˆ ุนุงูŠุฒ ุงู„ู€ Element ุงู„ุฎุงู…ุณุŒ ุจุฌู…ุน 5 ุนู„ู‰ ุฃูˆู„ ุนู†ูˆุงู†.

ู„ู…ุง ุจู†ูƒุชุจ array[index]ุŒ ุงู„ู€ Compiler ุจูŠุชุฑุฌู…ู‡ุง ู„ู€:

// Resolves to pointer arithmetic
*(array + index); 

ุจู…ุนู†ู‰: ู‡ุงุช ุฃูˆู„ ุนู†ูˆุงู† ูˆุงุฌู…ุน ุนู„ูŠู‡ ุงู„ู€ IndexุŒ ูˆุงู„ู„ูŠ ู‡ูŠุทู„ุน ุฏู‡ ุจูˆูŠู†ุชุฑุŒ ุญุท ู‚ุจู„ู‡ ุนู„ุงู…ุฉ ุงู„ู€ Asterisk * (ูˆุงู„ู„ูŠ ุจุชุนู…ู„ ุญุงุฌุฉ ุงุณู…ู‡ุง Dereference) ุนุดุงู† ุชุฌูŠุจ ุงู„ู‚ูŠู…ุฉ (Value) ุงู„ู„ูŠ ุงู„ู€ Pointer ุฏู‡ ุจูŠุดุงูˆุฑ ุนู„ูŠู‡ุง. ูˆู†ุงุฏุฑุงู‹ ู…ุง ู‡ู†ุญุชุงุฌ ู†ุชุนุงู…ู„ ู…ุน ุงู„ู€ Pointers ุจุดูƒู„ ู…ุจุงุดุฑ ูƒุฏู‡.


Function Parameters Passing

ุนู†ุฏู†ุง 4 ุทุฑู‚ ุนุดุงู† ู†ู…ุฑุฑ ุจูŠู‡ู… Parameters ู„ู„ู€ Function:

  1. Pass by Value: ุจุชุงุฎุฏ ู†ุณุฎุฉ (Copy) ู…ู† ุงู„ุฏุงุชุง ูˆุชุญุทู‡ุง ููŠ Variable ุฌุฏูŠุฏ ูˆุชุจุนุชู‡ ู„ู„ู€ Function. ู„ูˆ ุบูŠุฑุช ู‚ูŠู…ุชู‡ ุฌูˆู‡ุŒ ุงู„ู‚ูŠู…ุฉ ุงู„ุฃุตู„ูŠุฉ ุจุฑู‡ ุนู…ุฑู‡ุง ู…ุง ู‡ุชุชุบูŠุฑ.

    • ุงู„ู…ูŠุฒุฉ: ุงู„ู‚ูŠู…ุฉ ุงู„ุฃุตู„ูŠุฉ ู…ุญููˆุธุฉ.
    • ุงู„ุนูŠุจ: ุจูŠุณุชู‡ู„ูƒ ู…ูŠู…ูˆุฑูŠ ูƒุชูŠุฑ ูˆูƒู…ุงู† ูˆู‚ุช ( Time)ุŒ ุชุฎูŠู„ ู„ูˆ ุจุชุจุนุช Array ูƒุจูŠุฑ ุฌุฏุงู‹ุŒ ู‡ุชุถูŠุน ูˆู‚ุช ููŠ ู†ุณุฎ ูƒู„ Element ูˆูƒู…ุงู† ู‡ุชุณุชู‡ู„ูƒ ู…ูŠู…ูˆุฑูŠ ุฅุถุงููŠุฉ.
  2. Pass by Reference: ุจู†ุณุชุฎุฏู… ุงู„ู€ Reference Variable (ุจุนู„ุงู…ุฉ & ุฒูŠ int& x). ุงู„ู€ Variable ุฏู‡ ุจูŠุดุชุฑูƒ ู…ุน ุงู„ู€ Variable ุงู„ุฃุตู„ูŠ ููŠ ุงู„ู€ Symbol Table ุจูŠุดุงูˆุฑูˆุง ุนู„ู‰ ู†ูุณ ุงู„ุนู†ูˆุงู†. ู…ููŠุด ุญุงุฌุฉ ุงุณู…ู‡ุง ุฅู†ูƒ ุชุจุนุช ุงู„ู€ Variable ู†ูุณู‡ ููŠ C++ุŒ ุฅู†ุช ุจุชุจุนุช ุฑูŠูุฑู†ุณ ู„ูŠู‡.

    • ุงู„ู…ูŠุฒุฉ: ู…ููŠุด ุฃูŠ ุงุณุชู‡ู„ุงูƒ ู„ู„ูˆู‚ุช ุฃูˆ ุงู„ู…ูŠู…ูˆุฑูŠ.
    • ุงู„ุนูŠุจ: ู„ูˆ ุบูŠุฑุช ุฃูŠ ุญุงุฌุฉ ุฌูˆู‡ ุงู„ู€ FunctionุŒ ู‡ุชุชุบูŠุฑ ุจุฑู‡ ููŠ ุงู„ู‚ูŠู…ุฉ ุงู„ุฃุตู„ูŠุฉ.
  3. Pass by Pointer: ุจุฏู„ ู…ุง ุชุจุนุช ReferenceุŒ ุฅู†ุช ุจุชุจุนุช ุงู„ู€ Address ุจุชุงุน ุงู„ู€ Variable (ุฒูŠ int* x). ูˆู„ู…ุง ุชูŠุฌูŠ ุชู†ุงุฏูŠ ุงู„ู€ Function ุจุชุจุนุช &x. ุฌูˆู‡ ุงู„ู€ Function ุจุชุนู…ู„ Dereference ุนุดุงู† ุชุบูŠุฑ ุงู„ุฏุงุชุง. ู‡ูˆ ุจูŠุนู…ู„ ู†ูุณ ูˆุธูŠูุฉ ุงู„ู€ Reference ุจุงู„ุธุจุท ุจุณ ุงู„ู…ูŠูƒุงู†ูŠุฒู… ูˆุงู„ู€ Syntax ู…ุฎุชู„ู.

  4. Pass by Constant Reference: ุฒูŠ const int& x. ุฏูŠ ุงู„ุทุฑูŠู‚ุฉ ุงู„ู„ูŠ ู‡ู†ุณุชุฎุฏู…ู‡ุง ูƒุชูŠุฑ ุฌุฏุงู‹ ู…ุน ุงู„ู€ Vectors ูˆุงู„ู€ Maps. ุฅู†ุช ุจุชุจุนุชู‡ ูƒู€ Reference ุนุดุงู† ุชูˆูุฑ ุชุงูŠู… ูˆู…ูŠู…ูˆุฑูŠุŒ ุจุณ ุจุชุญุท const ุนุดุงู† ุชู…ู†ุน ุงู„ู€ Compiler ุฅู†ู‡ ูŠุณู…ุญู„ูƒ ุจุชุบูŠูŠุฑู‡ ุฌูˆู‡ ุงู„ู€ FunctionุŒ ุนุดุงู† ู…ุชุบู„ุทุด ูˆุชุบูŠุฑ ููŠู‡ ูˆูŠุณู…ุน ุจุฑู‡.


I-O Streams and Namespaces

ุนุดุงู† ู†ุงุฎุฏ Input ุฃูˆ ู†ุทุจุน Output ุจู†ุณุชุฎุฏู… cin ูˆ coutุŒ ูˆุฏูˆู„ ู…ูˆุฌูˆุฏูŠู† ููŠ Library ุงุณู…ู‡ุง <iostream>. ูˆู„ุงุฒู… ู†ุณุชุฎุฏู…:

using namespace std;

ุงู„ู€ Namespace ู‡ูˆ Logical Grouping ู„ุดูˆูŠู‡ Functions ุฃูˆ Classes ู„ูŠู‡ู… ุนู„ุงู‚ุฉ ุจุจุนุถ. ูุฅู†ุช ุจุชู‚ูˆู„ ู„ู€ C++ ุชุฌูŠุจ ุงู„ู€ Implementation ุจุชุงุน ุงู„ู€ cin ูˆุงู„ู€ cout ู…ู† ุงู„ู€ Namespace ุงู„ู„ูŠ ุงุณู…ู‡ std. ู…ุดูƒู„ุฉ ุฏู‡ ุฅู† ู…ู…ูƒู† ูŠุญุตู„ Conflict ู„ูˆ ุนู†ุฏูƒ ุฃูƒุชุฑ ู…ู† Namespace ููŠู‡ู… ู†ูุณ ุงู„ุฃุณุงู…ูŠุŒ ุจุณ ุฏู‡ ู…ุด ู‡ูŠุญุตู„ ููŠ ุงู„ุงุณูƒูˆุจ ุจุชุงุนู†ุง. ู„ูˆ ู…ุญุตู„ุด using ู‡ุชุถุทุฑ ุชูƒุชุจ std::cin.

ูƒู…ุงู† ููŠ ุญุงุฌุฉ ุงุณู…ู‡ุง ุงู„ู€ Cascading ููŠ ุงู„ู€ cin:

// Read multiple inputs
cin >> a >> b >> c; 

ุงู„ู€ Compiler ุจูŠุชุฑุฌู… ุฏูŠ ู„ู€ ูƒุฐุง cin ูˆุฑุง ุจุนุถ.


Standard Template Library (STL)

ุงู„ู€ C++ ุนู…ู„ุช ุซูˆุฑุฉ ูˆูˆูุฑุช Standard Template Library (STL)ุŒ ุฏูŠ ู…ูƒุชุจุฉ ููŠู‡ุง ุดูˆูŠุฉ Data Structures ุฌุงู‡ุฒุฉ (Containers) ุฒูŠ: Vector, Stack, Queue, Deque, Set, Map ูˆู‡ุชุณู‡ู„ ุนู„ูŠู†ุง ุญูŠุงุชู†ุง ุฌุฏุงู‹ ูˆู…ุด ู‡ู†ุถุทุฑ ู†ุจู†ูŠ ูƒู„ ุญุงุฌุฉ ู…ู† ุงู„ุตูุฑ. ุชุงู†ูŠ ุจู‚ูˆู„ูƒุŒ ุงูƒุชุจ ุจุงู„ู„ุบุฉ ุงู„ู„ูŠ ุชุฑูŠุญูƒ.

Iterator

ุงู„ู€ Iterators ุฒูŠ ุงู„ู€ Pointers ุจุงู„ุธุจุท ุจุณ ู…ุฎุตุตุฉ ู„ู„ู€ STL Containers. ุจู†ุนุฑูู‡ุง ุนุดุงู† ู†ุนู…ู„ Iterate ุนู„ู‰ ุงู„ู€ Elements. ู„ูŠู‡ ู…ุจู†ุณุชุฎุฏู…ุด ุงู„ู€ Index ุงู„ุนุงุฏูŠุŸ ู„ุฃู† ู…ุด ูƒู„ ุงู„ู€ Data Structures ุจุชูƒูˆู† Linear ุฃูˆ Contiguous ูˆุฑุง ุจุนุถ ููŠ ุงู„ู…ูŠู…ูˆุฑูŠ (ุฒูŠ ุงู„ู€ Linked List, Trees, Graphs, ุฃูˆ ุงู„ู€ Maps).

ูƒู„ Container (ุฒูŠ ุงู„ู€ Vector) ุฌูˆุงู‡ Methods ุจุชุณุงุนุฏู†ุง:

  • ุงู„ู€ .begin(): ุจูŠุดุงูˆุฑ ุนู„ู‰ ุฃูˆู„ Element.
  • ุงู„ู€ .end(): ุจูŠุดุงูˆุฑ ุนู„ู‰ ุงู„ู…ูƒุงู† ุงู„ู„ูŠ ุจุนุฏ ุขุฎุฑ Element.
  • ุงู„ู€ .rbegin(): ุจูŠุดุงูˆุฑ ุนู„ู‰ ุขุฎุฑ Element (ู„ู„ู€ Reverse).
  • ุงู„ู€ .rend(): ุจูŠุดุงูˆุฑ ุนู„ู‰ ุงู„ู…ูƒุงู† ุงู„ู„ูŠ ู‚ุจู„ ุฃูˆู„ Element.

ูˆู„ู…ุง ุจู†ุนู…ู„ Loop ุจู†ู‚ูˆู„:

// Loop until iterator reaches the end
for(auto it = v.begin(); it != v.end(); it++) { ... }

ุงู„ู€ it++ ุจุชู†ุท ู„ู„ู€ Element ุงู„ู„ูŠ ุจุนุฏู‡.

C#

ููŠ C++ ูƒู†ุง ุจู†ุณุชุฎุฏู… Iterators ุนุดุงู† ู†ุนู…ู„ traverse ุนู„ู‰ ุงู„ู€ STL Containers ุฒูŠ vector ูˆ map.
ููŠ C# ุงู„ููƒุฑุฉ ู‚ุฑูŠุจุฉ ุฌุฏู‹ุงุŒ ุจุณ ุจุฏู„ ู…ุง ู†ุณุชุฎุฏู… .begin() ูˆ .end() ุฅุญู†ุง ุนู†ุฏู†ุง:

ููŠ C# ู…ููŠุด ุญุงุฌุฉ ุงุณู…ู‡ุง .begin() ูˆ .end() ุตุฑูŠุญุฉุŒ ู„ูƒู† ุฃูŠ Collection ุฒูŠ List, Dictionary, LinkedList ุจุชimplement IEnumerableุŒ ูˆุจุงู„ุชุงู„ูŠ ู†ู‚ุฏุฑ ู†ุนู…ู„ ุนู„ูŠู‡ุง Iteration ุจุฃูƒุชุฑ ู…ู† ุทุฑูŠู‚ุฉ.

ุฏู‡ ุฃู‚ุฑุจ ุญุงุฌุฉ ู„ููƒุฑุฉ ุงู„ู€ Iterator ููŠ C++

List<int> numbers = new List<int> { 10, 20, 30, 40 };
 
// Get the enumerator (similar to begin())
IEnumerator<int> it = numbers.GetEnumerator();
 
// MoveNext() moves to the next element (similar to it++)
while (it.MoveNext())
{
	// Current gives the current element
	Console.WriteLine(it.Current);
}
  • ุงู„ู€ GetEnumerator() ุดุจู‡ begin()
  • ุงู„ู€ MoveNext() ุดุจู‡ it++
  • ุงู„ู€ Current ุดุจู‡ *it
  • ู…ููŠุด end() ุตุฑูŠุญุฉุŒ ู„ุฃู† MoveNext() ุจุชุฑุฌุน false ู„ู…ุง ู†ูˆุตู„ ู„ู„ุขุฎุฑ

ุชุงู†ูŠ ุทุฑูŠู‚ุฉ ูˆู‡ูŠ foreach

ุฏูŠ ุงู„ุทุฑูŠู‚ุฉ ุงู„ู„ูŠ ุจุชุณุชุฎุฏู… 99% ู…ู† ุงู„ูˆู‚ุช

List<int> numbers = new List<int> { 10, 20, 30, 40 };
 
// foreach internally uses an iterator
foreach (int item in numbers)
{
	Console.WriteLine(item);
}

ุงู„ู€ foreach ุฌูˆู‡ ุจูŠุณุชุฎุฏู… IEnumerator ุฃูˆุชูˆู…ุงุชูŠูƒุŒ
ูู‡ูˆ ู†ูุณ ููƒุฑุฉ ุงู„ู€ Iterator ุจุณ ุจุดูƒู„ ุฃู†ุถู ูˆุฃุณู‡ู„.


Reverse Iteration (Equivalent to rbegin / rend)

ููŠ C++ ูƒุงู† ุนู†ุฏู†ุง rbegin() ูˆ rend()
ููŠ C# ู†ุณุชุฎุฏู… Reverse() ู…ู† LINQ

List<int> numbers = new List<int> { 10, 20, 30, 40 };
 
// Reverse iteration using LINQ
foreach (int item in numbers.Reverse())
{
	Console.WriteLine(item);
}

ุงู„ู€ Reverse() ุจุชุฑุฌุน IEnumerable ู…ุนูƒูˆุณ
ูˆุจุงู„ุชุงู„ูŠ ู†ู‚ุฏุฑ ู†ุนู…ู„ ุนู„ูŠู‡ Iteration ุนุงุฏูŠ ุฌุฏู‹ุง.

ู„ูŠู‡ ู…ุด ุฏุงูŠู…ู‹ุง ู†ุณุชุฎุฏู… indexุŸ

ุนู„ุดุงู†:

  • ู…ุด ูƒู„ ุงู„ู€ Data Structures ุจุชุฏุนู… indexing (ุฒูŠ LinkedList)
  • ุจุนุถ ุงู„ู€ Collections ู…ุด Contiguous ููŠ memory
  • ุงู„ู€ Iterators ุจุชุฎู„ูŠู†ุง ู†ูƒุชุจ code generic ูŠุดุชุบู„ ู…ุน ุฃูŠ Collection

Pair & Vector

ุจุฏู„ ู…ุง ุงู„ู€ Variable ูŠุดูŠู„ ุฏุงุชุง ูˆุงุญุฏุฉุŒ ุงู„ู€ pair ุจูŠุฎู„ูŠู‡ ูŠุดูŠู„ ู‚ูŠู…ุชูŠู† ู…ุน ุจุนุถ.
ูŠุนู†ูŠ ุจุฏู„ ู…ุง ุชุนู…ู„ ู…ุชุบูŠุฑูŠู† ู…ุฑุชุจุทูŠู† ุจุจุนุถุŒ ุชู‚ุฏุฑ ุชุญุทู‡ู… ุฌูˆู‡ Object ูˆุงุญุฏ ู…ุฑุชุจุทูŠู† logically.

#include <iostream>
#include <utility>
using namespace std;
 
int main() {
    // Declare a pair of int and string
    pair<int, string> p;
 
    // Assign values
    p.first = 10;
    p.second = "Mahmoud";
 
    // Another way to initialize
    pair<int, int> p2 = make_pair(5, 7);
 
    cout << p.first << " " << p.second << endl;
    cout << p2.first + p2.second << endl;
 
    return 0;
}
  • ุงู„ู€ first ุจุชู…ุซู„ ุฃูˆู„ ู‚ูŠู…ุฉ
  • ุงู„ู€ second ุจุชู…ุซู„ ุชุงู†ูŠ ู‚ูŠู…ุฉ
  • ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู… make_pair() ุนุดุงู† ู†ุนู…ู„ Initialize ุจุณุฑุนุฉ

ุงู„ููƒุฑุฉ ูƒู„ู‡ุง ุฅู† ุงู„ู€ pair ุจูŠุฌู…ุน ู‚ูŠู…ุชูŠู† ู…ุฎุชู„ูุชูŠู† (ู…ู…ูƒู† ูŠุจู‚ูˆุง different types) ููŠ Object ูˆุงุญุฏ.


ุงู„ู€ vector ุฏู‡ Dynamic Array ุจุชุงุนู†ุง ูˆู‡ูŠุณู‡ู„ ุญูŠุงุชู†ุง. ู…ูŠุฒุชู‡ ุฅู†ูƒ ู…ุด ู…ุญุชุงุฌ ุชุญุฏุฏ ุงู„ุณุงูŠุฒ ุจุชุงุนู‡ุŒ ุจุณ ุทุจุนุงู‹ ู„ูˆ ุญุฏุฏุช ุงู„ุณุงูŠุฒ ู…ู† ุงู„ุฃูˆู„ ู„ูˆ ุฅู†ุช ุนุงุฑูู‡ุŒ ุฏู‡ ู‡ูŠุญุณู† ุงู„ู€ Performance ูˆู‡ูŠูˆูุฑ ุนู„ูŠูƒ ูˆู‚ุช ุงู„ู€ Resizing ุงู„ู„ูŠ ุจูŠุญุตู„ ููŠ ุงู„ุจุงูƒ ุฌุฑุงูˆู†ุฏ.

ุจู†ุถูŠู Elements ููŠู‡ ุจู€ push_back() ูˆุจู†ุฏุฎู„ ุนู„ูŠู‡ ุจุงู„ู€ Square Brackets [] ุนุงุฏูŠ ุฌุฏุงู‹. ุนุดุงู† ู†ุฌูŠุจ ุขุฎุฑ Element ุจู†ู‚ูˆู„ v[v.size() - 1].

ู…ู…ูƒู† ู†ุนุฑู ุงู„ู€ Vector ุจูƒุฐุง ุทุฑูŠู‚ุฉ:

// Initialize size with default values
vector<int> v(10); 
// Size 10, all values are 3
vector<int> v(10, 3); 

ูˆู„ูˆ ุนู†ุฏูƒ Array ุนุงุฏูŠ ูˆุนุงูŠุฒ ุชู†ู‚ู„ู‡ ู„ู€ Vector (ุนุดุงู† ู…ุซู„ุงู‹ ุชุณุชุฎุฏู… v.sort() ู„ุฃู† ุงู„ู€ Array ุงู„ุนุงุฏูŠ ู…ู„ูˆุด ุจูŠู„ุช ุฅู† Method ู„ู„ุณูˆุฑุช)ุŒ ู…ู…ูƒู† ุชุนู…ู„ู‡ ูƒุฏู‡:

// Copy from array to vector
vector<int> v(arr, arr + 5); 

C#

ุงู„ู€ Equivalent ู„ูŠู‡ุง ููŠ C# ู‡ูˆ ุงู„ู€ Tuple ุฃูˆ ุงู„ุฃูุถู„ ู…ู†ู‡ ุงู„ู€ ValueTuple.

ู…ุซุงู„ ููŠ C# ุจุงุณุชุฎุฏุงู… ValueTuple

using System;
 
class Program
{
    static void Main()
    {
        // Declare and initialize a tuple
        (int, string) p = (10, "Mahmoud");
 
        // Access values
        Console.WriteLine(p.Item1);
        Console.WriteLine(p.Item2);
 
        // Named tuple (cleaner)
        (int id, string name) person = (5, "Ali");
 
        Console.WriteLine(person.id);
        Console.WriteLine(person.name);
    }
}
  • ุงู„ู€ Item1 ุดุจู‡ first
  • ุงู„ู€ Item2 ุดุจู‡ second
  • ู†ู‚ุฏุฑ ู†ุฏูŠู‡ู… ุฃุณู…ุงุก ุนุดุงู† ุงู„ูƒูˆุฏ ูŠุจู‚ู‰ ุฃูˆุถุญ

ููŠ C# ุงู„ู€ Equivalent ู„ู„ู€ Vector ู‡ูˆ ุงู„ู€ List<T> ุงูˆ ุจู†ุณูŠู…ู‡ุง Generic List.

ุชุนุฑูŠู List ููŠ C#

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        // Empty list
        List<int> v = new List<int>();
 
        // Initialize with capacity (improves performance)
        List<int> v2 = new List<int>(10);
 
        // Initialize with default values (size 10, all zeros)
        List<int> v3 = new List<int>(new int[10]);
 
        // Initialize with same value repeated (like vector(10, 3))
        List<int> v4 = new List<int>();
        for (int i = 0; i < 10; i++)
            v4.Add(3);
    }
}

ุจู†ุถูŠู Elements ููŠู‡ ุจู€ Add() (ุจุฏู„ push_back())ุŒ
ูˆุจู†ุฏุฎู„ ุนู„ูŠู‡ ุจุงู„ู€ Square Brackets [] ุนุงุฏูŠ ุฌุฏุงู‹.

List<int> v = new List<int>();
 
// Add elements (similar to push_back)
v.Add(10);
v.Add(20);
 
// Access using index
Console.WriteLine(v[0]);
 
// Get last element
Console.WriteLine(v[v.Count - 1]);
  • ุงู„ู€ Add() ุดุจู‡ push_back()
  • ุงู„ู€ Count ุดุจู‡ size()
  • ุงู„ู€ v[v.Count - 1] ู†ูุณ ููƒุฑุฉ v[v.size() - 1]

ูˆู„ูˆ ุนู†ุฏูƒ Array ุนุงุฏูŠ ูˆุนุงูŠุฒ ุชุญูˆู„ู‡ ู„ู€ List:

int[] arr = { 1, 2, 3, 4, 5 };
 
// Copy from array to List
List<int> v = new List<int>(arr);

ูƒุฏู‡ ู†ู‚ู„ู†ุง ุงู„ุฏุงุชุง ู…ู† Array ู„ู€ ListุŒ
ูˆุจู‚ูŠู†ุง ู†ู‚ุฏุฑ ู†ุณุชุฎุฏู… Methods ุฒูŠ Sort():

v.Sort(); // Built-in sort

ุงู„ููƒุฑุฉ ุงู„ุนุงู…ุฉ ุฅู†:

  • ุงู„ู€ pair โ†” ValueTuple
  • ุงู„ู€ vector โ†” List<T>

ูˆุงู„ู€ Concept ูˆุงุญุฏ:
ุงู„ู€ Container ุฏูŠู†ุงู…ูŠูƒ ุจูŠูƒุจุฑ ู…ุนุงูƒุŒ ูˆุจูŠูˆูุฑู„ูƒ Built-in Methods ุชุณู‡ู„ ุนู„ูŠูƒ ุงู„ุชุนุงู…ู„ ู…ุน ุงู„ุฏุงุชุง.

Set & Map

  • ุงู„ู€ Set: ู‡ูˆ Container ุฃูŠ ุฏุงุชุง ุฌูˆุงู‡ ุจุชูƒูˆู† Sorted ูˆ Unique (ู…ููŠู‡ุงุด ุชูƒุฑุงุฑ). ู…ู† ุชุญุช ุงู„ู€ HoodุŒ ู‡ูŠ ุนุจุงุฑุฉ ุนู† Binary Search Tree (ุบุงู„ุจุงู‹ AVL ุฃูˆ Red-Black). ู…ุด ู‡ุชุนุฑู ุชุณุชุฎุฏู… ู…ุนุงู‡ุง IndexุŒ ู„ุงุฒู… Iterators (ุฃูˆ ุงู„ู€ for-each ุงู„ู„ูŠ ุงุดุชุบู„ุช ููŠ C++ ู…ู† ูƒุงู… ุณู†ุฉ).
  • ุงู„ู€ Multiset: ุฒูŠ ุงู„ู€ Set ุณูˆุฑุชุฏ ุจุณ ุจุชุณู…ุญ ุจุงู„ู€ Duplicates.
  • ุงู„ู€ Unordered Set: ุฏูŠ Unique ุจุณ ู…ุด Sorted (ุจุชุณุชุฎุฏู… Hashing).
  • ุงู„ู€ Map: ุฒูŠ ุงู„ู€ Set ุจุณ ุจุชุดูŠู„ Key ูˆ Value. ุงู„ูƒูŠุฒ ุจุชูƒูˆู† Sorted ูˆ Unique.
  • ุงู„ู€ Multimap: ุจุชุณู…ุญ ุจู€ Duplicates ููŠ ุงู„ูƒูŠุฒ.
  • ุงู„ู€ Unordered Map: ุฏูŠ ุงู„ู€ Hash Map ุงู„ุญู‚ูŠู‚ูŠุฉุŒ ู…ุด ุณูˆุฑุชุฏ ุจุณ ุณุฑูŠุนุฉ ุฌุฏุงู‹. ู‡ู†ุณุชุฎุฏู…ู‡ุง ูƒุชูŠุฑ ุฃูˆูŠ ููŠ ุงู„ู€ Dynamic Programming ุนุดุงู† ู†ุณุฌู„ ุงู„ู€ StatesุŒ ูˆุณุงุนุชู‡ุง ุนุดุงู† ู†ุฏูˆุฑ ุนู„ู‰ ูƒูŠ ู…ุนูŠู† ู‡ู†ู‚ูˆู„ map.find(key) != map.end() ุนุดุงู† ู†ุนุฑู ุงู„ูƒูŠ ู…ูˆุฌูˆุฏ ูˆู„ุง ู„ุฃ.

ุงู„ู€ Set

ุงู„ู€ Set ุจูŠุฎุฒู† Elements ุชูƒูˆู† Sorted ูˆ UniqueุŒ ูˆู…ููŠุด IndexingุŒ ูˆุงู„ู€ Traversal ุจูŠูƒูˆู† ุจุงู„ู€ Iterator ุฃูˆ for-each.

#include <iostream>
#include <set>
using namespace std;
 
int main() {
    set<int> s;
 
    // Insert elements
    s.insert(5);
    s.insert(1);
    s.insert(5); // duplicate, won't be inserted
 
    // Iterate using for-each
    for (int x : s) {
        cout << x << " ";
    }
 
    // Search
    if (s.find(1) != s.end()) {
        cout << "\nFound";
    }
 
    return 0;
}

ุงู„ู€ Multiset

ุฒูŠ ุงู„ู€ Set ุจุณ ุจูŠุณู…ุญ ุจุงู„ู€ Duplicates.

#include <iostream>
#include <set>
using namespace std;
 
int main() {
    multiset<int> ms;
 
    ms.insert(5);
    ms.insert(5);
    ms.insert(1);
 
    for (int x : ms) {
        cout << x << " ";
    }
 
    return 0;
}

ุงู„ู€ Unordered Set

ู‡ูŠ Unique ุจุณ ู…ุด SortedุŒ ุจูŠุนุชู…ุฏ ุนู„ู‰ Hashing.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main() {
    unordered_set<int> us;
 
    us.insert(10);
    us.insert(2);
    us.insert(10);
 
    for (int x : us) {
        cout << x << " ";
    }
 
    return 0;
}

ุงู„ู€ Map

ุจูŠุฎุฒู† Key-Value pairsุŒ ูˆุงู„ู€ Keys Sorted ูˆ Unique.

#include <iostream>
#include <map>
using namespace std;
 
int main() {
    map<string, int> mp;
 
    mp["Ali"] = 10;
    mp["Omar"] = 20;
 
    for (auto p : mp) {
        cout << p.first << " -> " << p.second << endl;
    }
 
    return 0;
}

ุงู„ู€ Multimap

ุจูŠุณู…ุญ ุจู€ Duplicates ููŠ ุงู„ู€ Keys.

#include <iostream>
#include <map>
using namespace std;
 
int main() {
    multimap<string, int> mm;
 
    mm.insert({"Ali", 10});
    mm.insert({"Ali", 20});
 
    for (auto p : mm) {
        cout << p.first << " -> " << p.second << endl;
    }
 
    return 0;
}

ุงู„ู€ Unordered Map

ุจูŠุนุชู…ุฏ ุนู„ู‰ HashingุŒ Lookup ุณุฑูŠุน.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main() {
    unordered_map<string, int> ump;
 
    ump["Ali"] = 100;
    ump["Omar"] = 200;
 
    if (ump.find("Ali") != ump.end()) {
        cout << "Found Ali\n";
    }
 
    return 0;
}

C#

SortedSet โ‡’ Set

ุจูŠุฎุฒู† Elements Sorted ูˆ UniqueุŒ ูˆู…ููŠุด Indexing ู…ุจุงุดุฑ.

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        SortedSet<int> s = new SortedSet<int>();
 
        // Add elements
        s.Add(5);
        s.Add(1);
        s.Add(5); // duplicate ignored
 
        foreach (int x in s)
        {
            Console.WriteLine(x);
        }
 
        // Search
        if (s.Contains(1))
        {
            Console.WriteLine("Found");
        }
    }
}

Multiset โ‡’using Dictionary (No direct equivalent)

ู…ููŠุด Multiset ุฌุงู‡ุฒุŒ ูุจู†ุณุชุฎุฏู… Dictionary<T,int> ู†ุฎุฒู† ููŠู‡ ุงู„ุนู†ุตุฑ ูˆุนุฏุฏ ุชูƒุฑุงุฑู‡.

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        Dictionary<int, int> multiset = new Dictionary<int, int>();
 
        int value = 5;
 
        if (!multiset.ContainsKey(value))
            multiset[value] = 0;
 
        multiset[value]++;
 
        foreach (var pair in multiset)
        {
            Console.WriteLine($"{pair.Key} count = {pair.Value}");
        }
    }
}

HashSet โ‡’ Unordered Set

ู‡ูŠ Unique ูˆู…ุด Sorted.

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        HashSet<int> hs = new HashSet<int>();
 
        hs.Add(10);
        hs.Add(2);
        hs.Add(10);
 
        foreach (int x in hs)
        {
            Console.WriteLine(x);
        }
    }
}

SortedDictionary โ‡’ Map

ุนุจุงุฑุฉ Key-Value pairsุŒ ูˆุงู„ู€ Keys Sorted ูˆ Unique.

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        SortedDictionary<string, int> mp = new SortedDictionary<string, int>();
 
        mp["Ali"] = 10;
        mp["Omar"] = 20;
 
        foreach (var pair in mp)
        {
            Console.WriteLine($"{pair.Key} -> {pair.Value}");
        }
    }
}

Multimap โ‡’ Dictionary<TKey, List> no direct equivalent

ู…ููŠุด Container ู…ุจุงุดุฑุŒ ูุจู†ุณุชุฎุฏู… List ุฌูˆุง Dictionary.

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        Dictionary<string, List<int>> multimap = new Dictionary<string, List<int>>();
 
        string key = "Ali";
 
        if (!multimap.ContainsKey(key))
            multimap[key] = new List<int>();
 
        multimap[key].Add(10);
        multimap[key].Add(20);
 
        foreach (var pair in multimap)
        {
            foreach (var value in pair.Value)
            {
                Console.WriteLine($"{pair.Key} -> {value}");
            }
        }
    }
}

Dictionary โ‡’ Unordered Map

ุจูŠุนุชู…ุฏ ุนู„ู‰ Hashing ูˆ Lookup ุณุฑูŠุน.

using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        Dictionary<string, int> ump = new Dictionary<string, int>();
 
        ump["Ali"] = 100;
        ump["Omar"] = 200;
 
        if (ump.ContainsKey("Ali"))
        {
            Console.WriteLine("Found Ali");
        }
    }
}

STL Algorithms

ููŠ ุจูŠู„ุช ุฅู† Functions ู…ู‡ู…ุฉ ุฌุฏู‹ุง ููŠ ุงู„ู€ STL:

  • ุงู„ู€ sort(v.begin(), v.end()): ุจุชุนู…ู„ Sorting ู„ู„ู€ Range ูƒู„ู‡ุŒ ูˆุจุชุดุชุบู„ ููŠ ู…ุชูˆุณุท ุญุงู„ุฉ ุจู€ O(N log N).
  • ุงู„ู€ binary_search: ู„ุงุฒู… ุชุดุชุบู„ ุนู„ู‰ Container ูŠูƒูˆู† Sorted ุงู„ุฃูˆู„. ููƒุฑุชู‡ุง ุฅู†ู‡ุง ุจุชู‚ุณู… ุงู„ู€ Search Space ู†ุตูŠู† ูƒู„ ู…ุฑุฉุŒ ููƒู„ Step ุจุชู„ุบูŠ ู†ุต ุงู„ุงุญุชู…ุงู„ุงุชุŒ ูˆุนู„ุดุงู† ูƒุฏู‡ ุงู„ู€ Complexity ุจุชุงุนุชู‡ุง O(log N).
  • ุงู„ู€ lower_bound: ุจุชุฑุฌุน Iterator ุนู„ู‰ ุฃูˆู„ Element ุฃูƒุจุฑ ู…ู† ุฃูˆ ูŠุณุงูˆูŠ ุงู„ู‚ูŠู…ุฉ ุงู„ู„ูŠ ุจุชุฏูˆุฑ ุนู„ูŠู‡ุง.
  • ุงู„ู€ upper_bound: ุจุชุฑุฌุน Iterator ุนู„ู‰ ุฃูˆู„ Element ุฃูƒุจุฑ strictly ู…ู† ุงู„ู‚ูŠู…ุฉ.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int main() {
    vector<int> v = {5, 1, 4, 2, 3};
 
    // Sort the vector
    sort(v.begin(), v.end());
 
    // Binary search (returns true/false)
    if (binary_search(v.begin(), v.end(), 3)) {
        cout << "Found 3\n";
    }
 
    // lower_bound
    auto lb = lower_bound(v.begin(), v.end(), 3);
    cout << "lower_bound: " << *lb << endl;
 
    // upper_bound
    auto ub = upper_bound(v.begin(), v.end(), 3);
    cout << "upper_bound: " << *ub << endl;
 
    return 0;
}

ุงู„ูุฑู‚ ุงู„ู…ู‡ู…:

  • ุงู„ู€ binary_search ุจูŠุฑุฌุน bool.
  • ุงู„ู€ lower_bound ูˆ upper_bound ุจูŠุฑุฌุนูˆุง Iterator.
  • ุชู‚ุฏุฑ ุชุณุชุฎุฏู… ุงู„ูุฑู‚ ุจูŠู†ู‡ู… ุนุดุงู† ุชุฌูŠุจ ุนุฏุฏ ุงู„ุชูƒุฑุงุฑุงุช:
    count = upper_bound - lower_bound.

C#

ููŠ C# ุงู„ู€ Equivalent ู…ูˆุฌูˆุฏ ููŠ List<T> ูˆ ArrayุŒ ูˆูƒู…ุงู† ููŠ LINQ.

  • ุงู„ู€ Sort(): ู…ูˆุฌูˆุฏุฉ ุฌูˆู‡ List<T> ูˆ Array.Sort()ุŒ ูˆุจุชุดุชุบู„ ุจู€ O(N log N).
  • ุงู„ู€ BinarySearch(): ู…ูˆุฌูˆุฏุฉ ููŠ List<T> ูˆ ArrayุŒ ูˆู„ุงุฒู… ุงู„ุฏุงุชุง ุชูƒูˆู† Sorted ุงู„ุฃูˆู„ุŒ ูˆุจุชุดุชุบู„ ุจู€ O(log N).
  • ู…ููŠุด lower_bound ูˆ upper_bound Built-in ุจู†ูุณ ุงู„ุงุณู…ุŒ ู„ูƒู† ุชู‚ุฏุฑ ุชุณุชุฎุฏู… BinarySearch() ูˆุชุฑุชุจ ุงู„ู†ุชูŠุฌุฉ ุนุดุงู† ุชูˆุตู„ ู„ู†ูุณ ุงู„ููƒุฑุฉ.
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        List<int> v = new List<int> { 5, 1, 4, 2, 3 };
 
        // Sort the list
        v.Sort();
 
        // Binary search
        int index = v.BinarySearch(3);
 
        if (index >= 0)
        {
            Console.WriteLine("Found 3 at index " + index);
        }
    }
}

ู…ู‡ู… ุชุนุฑู ุฅู†:

  • ุงู„ู€ BinarySearch() ุจูŠุฑุฌุน Index ู„ูˆ ู„ู‚ู‰ ุงู„ุนู†ุตุฑ.
  • ู„ูˆ ู…ู„ู‚ู‡ูˆุด ุจูŠุฑุฌุน ุฑู‚ู… ุณุงู„ุจ (negative insertion index).
  • ู…ููŠุด Iterator ููŠ C#ุŒ ูุฅู†ุช ุจุชุชุนุงู…ู„ ุจุงู„ู€ Index.

ููŠ C++ ูƒู†ุง ุจู†ุณุชุฎุฏู… ุงู„ู€ lower_bound ูˆ upper_bound ุฌุงู‡ุฒูŠู†.
ููŠ C# ู…ููŠุด ู†ูุณ ุงู„ู€ API ุจุงู„ุงุณู…ุŒ ูู…ู‡ู… ุชุจู‚ู‰ ุนุงุฑู ุชุนู…ู„ู‡ู… ุจุฅูŠุฏูƒุŒ ูˆุฏู‡ ุจูŠุชุณุฃู„ ูƒุชูŠุฑ ููŠ ุงู„ุงู†ุชุฑููŠูˆุฒ.

ุงู„ููƒุฑุฉ ุงู„ุฃุณุงุณูŠุฉ:

  • ุงู„ู€ lower_bound: ุฃูˆู„ Index ุงู„ุนู†ุตุฑ ููŠู‡ >= target
  • ุงู„ู€ upper_bound: ุฃูˆู„ Index ุงู„ุนู†ุตุฑ ููŠู‡ > target

ูˆุงู„ุงุชู†ูŠู† ู„ุงุฒู… ูŠุดุชุบู„ูˆุง ุนู„ู‰ Array ุฃูˆ List ุชูƒูˆู† Sorted.


ุงู„ู€ lower_bound

// Returns first index where value >= target
static int LowerBound(List<int> arr, int target)
{
    int left = 0;
    int right = arr.Count; // notice: not Count - 1
 
    while (left < right)
    {
        int mid = left + (right - left) / 2;
 
        if (arr[mid] < target)
            left = mid + 1;
        else
            right = mid;
    }
 
    return left;
}

ุฅุญู†ุง ู‡ู†ุง ุจู†ุถูŠู‚ ุงู„ู€ Search Space ู„ุญุฏ ู…ุง ู†ูˆุตู„ ู„ุฃูˆู„ ู…ูƒุงู† ูŠู†ูุน ูŠุชุญุท ููŠู‡ ุงู„ู€ target ู…ู† ุบูŠุฑ ู…ุง ูŠูƒุณุฑ ุงู„ุชุฑุชูŠุจ.
ู„ูˆ ุงู„ุนู†ุตุฑ ู…ุด ู…ูˆุฌูˆุฏุŒ ุจูŠุฑุฌุนู„ูƒ ู…ูƒุงู† ุฅุฏุฎุงู„ู‡ ุงู„ุตุญ.


ุงู„ู€ upper_bound

// Returns first index where value > target
static int UpperBound(List<int> arr, int target)
{
    int left = 0;
    int right = arr.Count;
 
    while (left < right)
    {
        int mid = left + (right - left) / 2;
 
        if (arr[mid] <= target)
            left = mid + 1;
        else
            right = mid;
    }
 
    return left;
}
  • ููŠ LowerBound ุงู„ุดุฑุท:
    arr[mid] < target
  • ููŠ UpperBound ุงู„ุดุฑุท:
    arr[mid] <= target

ูˆุฏู‡ ุงู„ูุฑู‚ ุงู„ู„ูŠ ุจูŠุญุฏุฏ ู‡ู„ ู‡ุชู‚ู ุนู†ุฏ ุฃูˆู„ ู…ุณุงูˆูŠ ูˆู„ุง ุฃูˆู„ ุฃูƒุจุฑ.


using System;
using System.Collections.Generic;
 
class Program
{
    static int LowerBound(List<int> arr, int target)
    {
        int left = 0;
        int right = arr.Count;
 
        while (left < right)
        {
            int mid = left + (right - left) / 2;
 
            if (arr[mid] < target)
                left = mid + 1;
            else
                right = mid;
        }
 
        return left;
    }
 
    static int UpperBound(List<int> arr, int target)
    {
        int left = 0;
        int right = arr.Count;
 
        while (left < right)
        {
            int mid = left + (right - left) / 2;
 
            if (arr[mid] <= target)
                left = mid + 1;
            else
                right = mid;
        }
 
        return left;
    }
 
    static void Main()
    {
        List<int> v = new List<int> { 1, 2, 2, 2, 4, 5 };
 
        int lb = LowerBound(v, 2);
        int ub = UpperBound(v, 2);
 
        Console.WriteLine("LowerBound index: " + lb);
        Console.WriteLine("UpperBound index: " + ub);
        Console.WriteLine("Count of 2 = " + (ub - lb));
    }
}
  • ุงู„ุงุชู†ูŠู† O(log N)
  • ู†ูุณ ููƒุฑุฉ ุงู„ู€ Binary Search
  • ุดุบุงู„ูŠู† ุนู„ู‰ Sorted Array ุจุณ

Data Structures Representation

Tree

ุงู„ู€ Tree ุนุจุงุฑุฉ ุนู† NodeุŒ ูƒู„ Node ุดุงูŠู„ุฉ ุงู„ุฏุงุชุง ูˆุจูˆูŠู†ุชุฑุฒ ู„ู„ู€ Children.
ู„ูˆ Binary Tree ุจูŠุจู‚ู‰ ุนู†ุฏู†ุง left ูˆ right.
ู„ูˆ Generic Tree ุจู†ุณุชุฎุฏู… vector ู…ู† ุงู„ู€ Nodes ุจุฏู„ Array ุนุงุฏูŠุฉ.

Binary Tree Implementation

#include <iostream>
using namespace std;
 
struct Node {
    int data;
    Node* left;
    Node* right;
 
    Node(int value) {
        data = value;
        left = nullptr;
        right = nullptr;
    }
};
 
int main() {
    Node* root = new Node(1);
 
    root->left = new Node(2);
    root->right = new Node(3);
 
    cout << root->left->data << endl;
 
    return 0;
}

Generic Tree Implementation

#include <iostream>
#include <vector>
using namespace std;
 
struct Node {
    int data;
    vector<Node*> children;
 
    Node(int value) {
        data = value;
    }
};
 
int main() {
    Node* root = new Node(1);
 
    root->children.push_back(new Node(2));
    root->children.push_back(new Node(3));
 
    cout << root->children[0]->data << endl;
 
    return 0;
}

Graph

ุนู†ุฏู†ุง ุทุฑูŠู‚ุชูŠู† ู„ุชู…ุซูŠู„ ุงู„ู€ Graph.

Adjacency List

ูƒู„ Node ู„ูŠู‡ุง List ู…ู† ุงู„ู€ Neighbors.

#include <iostream>
#include <vector>
using namespace std;
 
int main() {
    int n = 5;
    vector<vector<int>> adj(n);
 
    // Add edge between 0 and 1
    adj[0].push_back(1);
    adj[1].push_back(0);
 
    for (int neighbor : adj[0]) {
        cout << neighbor << " ";
    }
 
    return 0;
}

Adjacency Matrix

ุงู„ู€ 2D Array ุจุชู…ุซู„ ูˆุฌูˆุฏ Edge.

#include <iostream>
#include <vector>
using namespace std;
 
int main() {
    int n = 5;
    vector<vector<int>> matrix(n, vector<int>(n, 0));
 
    // Add edge between 0 and 1
    matrix[0][1] = 1;
    matrix[1][0] = 1;
 
    if (matrix[0][1] == 1) {
        cout << "Edge exists";
    }
 
    return 0;
}

  • ุงู„ู€ Matrix:
    • Space = O(Nยฒ)
    • Check edge = O(1)
  • ุงู„ู€ List:
    • Space = O(N + E)
    • Check edge = O(degree)

C#

Trees

ููŠ C# ุจุฏู„ ุงู„ุจูˆูŠู†ุชุฑุฒ ุจู†ุณุชุฎุฏู… References ุนุงุฏูŠ.

Binary Tree
using System;
 
class Node
{
    public int Data;
    public Node Left;
    public Node Right;
 
    public Node(int value)
    {
        Data = value;
    }
}
 
class Program
{
    static void Main()
    {
        Node root = new Node(1);
        root.Left = new Node(2);
        root.Right = new Node(3);
 
        Console.WriteLine(root.Left.Data);
    }
}

Generic Tree
using System;
using System.Collections.Generic;
 
class Node
{
    public int Data;
    public List<Node> Children = new List<Node>();
 
    public Node(int value)
    {
        Data = value;
    }
}
 
class Program
{
    static void Main()
    {
        Node root = new Node(1);
 
        root.Children.Add(new Node(2));
        root.Children.Add(new Node(3));
 
        Console.WriteLine(root.Children[0].Data);
    }
}

Graphs

Adjacency List
using System;
using System.Collections.Generic;
 
class Program
{
    static void Main()
    {
        int n = 5;
        List<int>[] adj = new List<int>[n];
 
        for (int i = 0; i < n; i++)
            adj[i] = new List<int>();
 
        adj[0].Add(1);
        adj[1].Add(0);
 
        foreach (int neighbor in adj[0])
        {
            Console.WriteLine(neighbor);
        }
    }
}

Adjacency Matrix
using System;
 
class Program
{
    static void Main()
    {
        int n = 5;
        int[,] matrix = new int[n, n];
 
        matrix[0, 1] = 1;
        matrix[1, 0] = 1;
 
        if (matrix[0, 1] == 1)
        {
            Console.WriteLine("Edge exists");
        }
    }
}

Stack vs. Heap Memory

Stack

ุงู„ู€ Stack ู…ุณุงุญุฉ ุงู„ู…ูŠู…ูˆุฑูŠ ุงู„ู„ูŠ ุจุชุณุชุฎุฏู… ู„ุชุฎุฒูŠู† ุงู„ู€ Local Variables ูˆุงู„ู€ Function Calls.
ู…ุณุงุญุชู‡ Limited ูˆุตุบูŠุฑุฉ ู…ู‚ุงุฑู†ุฉ ุจุงู„ู€ HeapุŒ ูˆู…ููŠุด ุญุงุฌุฉ ุงุณู…ู‡ุง Resize.

  • ุงู„ุชุฎุฒูŠู† ููŠู‡ ุจูŠูƒูˆู† Automatic.
  • ุฃูˆู„ ู…ุง ุงู„ู€ Scope ูŠุฎู„ุตุŒ ุงู„ู…ูŠู…ูˆุฑูŠ ุจุชุชูุถูŠ ู„ูˆุญุฏู‡ุง.
  • ุจูŠุฎุฒู†:
    • ุงู„ู€ Primitive Variables (ุฒูŠ int, double).
    • ุงู„ู€ References ู†ูุณู‡ุง (ู…ุด ุงู„ู€ Objects).
    • ุงู„ู€ Function Call Frames ููŠ ุญุงู„ุฉ ุงู„ู€ Recursion.

ู…ุซุงู„ C++:

void func() {
    int x = 10; // stored in Stack
}

ู…ุซุงู„ C#:

void Func()
{
    int x = 10; // stored in Stack
}

ูƒู„ Call ู„ู„ู€ Function ุจูŠุนู…ู„ Stack Frame ุฌุฏูŠุฏ.


Heap

ุงู„ู€ Heap ู…ุณุงุญุฉ ุงู„ู…ูŠู…ูˆุฑูŠ ุงู„ู„ูŠ ุจู†ุณุชุฎุฏู…ู‡ุง ููŠ ุงู„ู€ Dynamic Allocation ูˆู‚ุช ุงู„ู€ Run Time.

  • ู…ุณุงุญุชู‡ ุฃูƒุจุฑ ุจูƒุชูŠุฑ.
  • ุงู„ุชุฎุฒูŠู† ููŠู‡ Manual ููŠ C++ (ู„ุงุฒู… ุชุนู…ู„ delete).
  • ููŠ C# ุจูŠูƒูˆู† Automatic ุนู† ุทุฑูŠู‚ ุงู„ู€ Garbage Collector.
  • ุจูŠุฎุฒู† ุงู„ู€ Objects ูˆุงู„ู€ Dynamic Data Structures.

ู…ุซุงู„ C++:

int* p = new int(5); // allocated in Heap
delete p;            // must free manually

ู…ุซุงู„ C#:

int[] arr = new int[5]; // array stored in Heap
// GC will free it automatically

ู†ู‚ุทุฉ ู…ู‡ู…ุฉ ููŠ ุงู„ุงู†ุชุฑููŠูˆ

ุงู„ู€ vector ููŠ C++:

  • ุงู„ู€ Object ู†ูุณู‡ (ุงู„ุญุฌู…ุŒ ุงู„ูƒุงุจุงุณูŠุชูŠุŒ ุงู„ุจูˆูŠู†ุชุฑ) ุจูŠุชุฎุฒู† ููŠ ุงู„ู€ Stack ู„ูˆ ู…ุชุนุฑู Local.
  • ู„ูƒู† ุงู„ู€ Elements ู†ูุณู‡ุง ุจุชุชุญุฌุฒ ููŠ ุงู„ู€ Heap.

ู…ุซุงู„:

vector<int> v(100);
  • ุงู„ู€ v ู†ูุณู‡ ููŠ ุงู„ู€ Stack.
  • ุงู„ู€ 100 ุนู†ุตุฑ ููŠ ุงู„ู€ Heap.

ู†ูุณ ุงู„ููƒุฑุฉ ููŠ C# ู…ุน List<T>:

List<int> list = new List<int>(100);
  • ุงู„ู€ Reference ููŠ ุงู„ู€ Stack.
  • ุงู„ู€ Internal Array ููŠ ุงู„ู€ Heap.

ุบู„ุทุฉ ู…ุดู‡ูˆุฑุฉ ููŠ ุงู„ุงู†ุชุฑููŠูˆ (Recursion)

ู„ูˆ ูƒุชุจุช ุญู„ ุจู€ Recursion:

int sum(int n) {
    if (n == 0) return 0;
    return n + sum(n - 1);
}

ููŠ ู†ุงุณ ุชู‚ูˆู„ Space Complexity = O(1) ุนุดุงู† ู…ููŠุด Array ุฌุฏูŠุฏ.

ุฏู‡ ุบู„ุท.

ู„ูŠู‡ุŸ

ู„ุฃู† ูƒู„ Recursive Call ุจูŠุชุญุท ููŠ ุงู„ู€ Stack ูƒู€ Stack Frame.
ู„ูˆ ุนู†ุฏูƒ N CallsุŒ ูŠุจู‚ู‰ ุนู†ุฏูƒ N Frames.

ูู€:

  • Time Complexity = O(N)
  • ุงู„ู€ Space Complexity = O(N) ุจุณุจุจ ุนู…ู‚ ุงู„ู€ Recursion

ููŠ C# ู†ูุณ ุงู„ููƒุฑุฉ:

static int Sum(int n)
{
    if (n == 0) return 0;
    return n + Sum(n - 1);
}

ูƒู„ Call ุจุชุชุฒูˆุฏ ููŠ ุงู„ู€ Call Stack.


Summary

FeatureStackHeap
SizeLimitedLarge
ManagementAutomaticManual (C++) / GC (C#)
Allocation TimeCompile Time (mostly)Run Time
Used ForLocal variables, Call stackObjects, Dynamic structures
ResizeNoYes
  • ุงู„ู€ Stack: ู…ุณุงุญุชู‡ Limited ุฌุฏุงู‹ ูˆุฃู‚ู„ ู…ู† ุงู„ู€ HeapุŒ ู…ููŠุด ุญุงุฌุฉ ุงุณู…ู‡ุง Resize ู„ูŠู‡. ุจูŠุชุฎุฒู† ููŠู‡ ุงู„ุญุงุฌุงุช ุงู„ู€ Static ุงู„ู„ูŠ ุจุชุชุญุฌุฒ ููŠ ุงู„ู€ Compile Time. ูˆุจูŠูƒูˆู† Automatic ูŠุนู†ูŠ ุงู„ู€ Compiler ุนุงุฑู ุฅู…ุชู‰ ูŠุญุฌุฒ ูˆุฅู…ุชู‰ ูŠูุถูŠ ู…ุฌุฑุฏ ู…ุง ุงู„ู€ Scope ุจุชุงุน ุงู„ู€ Variable ูŠุฎู„ุต.
  • ุงู„ู€ Heap: ุจู†ุนู…ู„ ููŠู‡ Dynamic Allocation ููŠ ุงู„ู€ Run TimeุŒ ูˆู„ุงุฒู… ุชูุถูŠู‡ ุจู†ูุณูƒุŒ ูˆู„ูˆ ู…ูุถุชูˆุด ู…ุด ู‡ูŠูุถู‰ ุบูŠุฑ ู„ู…ุง ุงู„ุจุฑู†ุงู…ุฌ ูƒู„ู‡ ูŠุฎู„ุต ูˆุงู„ู€ OS ูŠู†ุถู ูˆุฑุงูƒ. ู„ูˆ ุงู„ูƒูˆุฏ ุจุชุงุนูƒ ูƒุจูŠุฑ ูˆุนู…ุงู„ ุชุญุฌุฒ ู…ูŠู…ูˆุฑูŠ ูˆู…ุจุชูุถูŠุดุŒ ุงู„ู€ Heap ู‡ุชุชู…ู„ูŠ.

ู…ู„ุญูˆุธุฉ ู…ู‡ู…ุฉ: ุงู„ู€ Vector ุงู„ู€ Header ุจุชุงุนู‡ (ุงุณู…ู‡ ูˆุงู„ุณุงูŠุฒ) ุจูŠุชุญุท ููŠ ุงู„ู€ StackุŒ ุจุณ ุงู„ู€ Elements ู†ูุณู‡ุง ุจุชุชุญุท ููŠ ุงู„ู€ Heap. ูู„ูˆ ุณุฃู„ูˆูƒ ููŠ ุงู„ุงู†ุชุฑููŠูˆ ุนู† ุงู„ู€ Space Complexity ู…ู…ูƒู† ุชูุตู„ู‡ู… ุชู‚ูˆู„ู‡ ุงู„ุณุชุงูƒ ูƒุฐุง ูˆุงู„ู‡ูŠุจ ูƒุฐุง.

ุบู„ุทุฉ ู…ุดู‡ูˆุฑุฉ ููŠ ุงู„ุงู†ุชุฑููŠูˆ: ุญุฏ ูŠูƒุชุจ ุญู„ ุจู€ RecursionุŒ ูˆู„ู…ุง ูŠุชุณุฃู„ ุนู† ุงู„ู€ Space Complexity ูŠู‚ูˆู„ ุจุญุฌุฉ ุฅู†ู‡ ู…ุนู…ู„ุด Array ุฌุฏูŠุฏ! ุฏู‡ ุบู„ุทุŒ ู„ุฃู† ุฅู†ุช ุนู…ู„ุช N Recursive CallsุŒ ูˆูƒู„ ูƒูˆู„ ุจุชุชุญุท ููŠ ุงู„ู€ StackุŒ ูุงู„ู€ Space Complexity ุจุชุงุนุชูƒ ุจุชูƒูˆู† ุจู†ุงุกู‹ ุนู„ู‰ ุนู…ู‚ ุงู„ู€ Recursion.


Common Mistakes & Time Limits

Integer Overflow

ุงู„ุณุงูŠุฒ ุจุชุงุน ุงู„ู€ Integer ููŠ ุฃุบู„ุจ ุงู„ุจุฑูˆุณูŠุณูˆุฑุฒ ู‡ูˆ 32 Bits (ุจูŠุดูŠู„ ู„ุญุฏ ). ู„ูˆ ุฌูŠุช ุชุถุฑุจ ุฑู‚ู…ูŠู† ูƒู„ ูˆุงุญุฏ 50,000ุŒ ู‡ุชุญุตู„ ู…ุดูƒู„ุฉ ุงู„ู€ Overflow ูˆุงู„ู†ุงุชุฌ ู‡ูŠู„ู (Wraparound) ูˆูŠุฌูŠุจู„ูƒ ุฃุฑู‚ุงู… ุจุงู„ุณุงู„ุจ. ุนุดุงู† ุชุญู„ู‡ุง:

// Avoid integer overflow
long long res = (long long)a * b;

ู„ุงุฒู… ุงู„ู€ Operation ู†ูุณู‡ุง ุชุญุตู„ ูƒู€ long long ู…ุด ุงู„ู€ Variable ุงู„ู†ู‡ุงุฆูŠ ุจุณุŒ ุนุดุงู† ู„ูˆ ุงุชุถุฑุจูˆุง ูƒู€ int ู‡ูŠุญุตู„ Overflow ู‚ุจู„ ู…ุง ูŠุชุณุฌู„ูˆุง.

Time Limit Exceeded (TLE)

ุฃุบู„ุจ ุงู„ู€ Processors ุจุชุฑู† ุญูˆุงู„ูŠ Instruction ููŠ ุงู„ุซุงู†ูŠุฉ. ูˆุงู„ู€ Online Judges (ุฒูŠ LeetCode) ุจุชุฏูŠ ุชุงูŠู… ู„ูŠู…ุช ู…ู† ุซุงู†ูŠุฉ ู„ุซุงู†ูŠุชูŠู†. ูู„ูˆ ุงู„ู…ุณุฃู„ุฉ ุงู„ูƒูˆู†ุณุฑุชูŠู†ุฒ ุจุชุงุนุชู‡ุง ุŒ ูˆุงู„ุญู„ ุจุชุงุนูƒ ุŒ ุฏู‡ ู…ุนู†ุงู‡ ุนู…ู„ูŠุงุชุŒ ูˆุฏู‡ ุฃูƒุจุฑ ุจูƒุชูŠุฑ ู…ู† ุŒ ูุฃูƒูŠุฏ ู‡ูŠุฌูŠู„ูƒ TLE. ุณุงุนุชู‡ุง ู‡ุชุนุฑู ุฅู† ุญู„ูƒ ู…ุด ู‡ูŠู†ูุน ูˆู„ุงุฒู… ุชููƒุฑ ููŠ ุญู„ ุฃุณุฑุน ุฒูŠ ุฃูˆ .

Undefined Behavior

ู„ูˆ ุนู…ู„ุช Access ู„ู€ array[-1]ุŒ ุงู„ู€ Compiler ู‡ูŠู€ Dereference ุงู„ู…ูŠู…ูˆุฑูŠ ุงู„ู„ูŠ ู‚ุจู„ ุฃูˆู„ Element ููŠ ุงู„ู€ Array! ุฏูŠ ู…ูŠู…ูˆุฑูŠ ุฅู†ุช ู…ุด ู…ุณุคูˆู„ ุนู†ู‡ุง ูˆููŠู‡ุง Garbage Values. ุฏู‡ ุจูŠุณู…ูˆู‡ Undefined Behavior. ู†ูุณ ุงู„ูƒู„ุงู… ู„ูˆ ู…ุนู…ู„ุชุด Initialize ู„ู€ Variable.

Output Formatting

ุงู„ู…ุณุฃู„ุฉ ุจุชุฏูŠู„ูƒ Input ูˆุจุชุทู„ุจ Output ุจุตูŠุบุฉ ู…ุนูŠู†ุฉ. ู…ุชุทุจุนุด โ€œThe answer of the problem isโ€ฆโ€œ. ุงุทุจุน ุงู„ุฑู‚ู… ุฃูˆ ุงู„ุญุงุฌุฉ ุงู„ู…ุทู„ูˆุจุฉ ุจุณ ุนุดุงู† ุชุนุฏูŠ ู…ู† ุงู„ู…ุณุฃู„ุฉ. ุดุบู„ โ€œPlease enter the numberโ€ ุฏู‡ ุจุชุงุน ุณู†ุฉ ุฃูˆู„ู‰ ูƒู„ูŠุฉ ู…ุจู†ุนู…ู„ูˆุด ู‡ู†ุง.


Problem Solving & Interview Strategies

Trust the Constraints

ุงู„ู…ุณุฃู„ุฉ ุจุชุฏูŠูƒ ConstraintsุŒ ู…ุซู„ุงู‹ ู…ู† ุตูุฑ ู„ู€ 10,000. ู…ุชุฌูŠุด ุฅู†ุช ุชูƒุชุจ ููŠ ุฃูˆู„ ุณุทุฑ if (x < 0) return;. ู„ูŠู‡ ุงู„ู€ Trust Issues ุฏูŠุŸ ุทุงู„ู…ุง ู‚ุงู„ูƒ ู…ููŠุด ุฃู‚ู„ ู…ู† ุฒูŠุฑูˆ ูŠุจู‚ู‰ ู…ููŠุด. ุงู„ู€ Valdiations ุงู„ุฒูŠุงุฏุฉ ุฏูŠ ุจุชุนู…ู„ Overcomplicating ู„ู„ูƒูˆุฏุŒ ูˆุจุชุตุนุจ ุนู„ูŠูƒ ุงู„ู€ Debugging ุฌุฏุงู‹ุŒ ูˆุงู„ุงู†ุชุฑููŠูˆุฑ ู‡ูŠุฒู‡ู‚ ู…ู†ูƒ ูˆุงู„ูˆู‚ุช ู‡ูŠุฎู„ุต. Stick to the requirements!

Donโ€™t Make Assumptions

ู…ุชูุชุฑุถุด ุญุงุฌุฉ ู…ุด ู…ูƒุชูˆุจุฉ. ู…ุญุฏุด ุฌุงุจู„ูƒ ุณูŠุฑุฉ ุฅู† ุงู„ู€ Array ุณูˆุฑุชุฏ ูŠุจู‚ู‰ ู…ุด ุณูˆุฑุชุฏ. ูƒู„ ูƒู„ู…ุฉ ููŠ ุงู„ู€ Problem Description ู…ูƒุชูˆุจุฉ ู„ุณุจุจ. ู„ูˆ ู‚ุงู„ูƒ ู…ููŠุด ุฃุฑู‚ุงู… NegativeุŒ ูŠุจู‚ู‰ ู…ู…ูƒู† ูŠูƒูˆู† ููŠู‡ ุฒูŠุฑูˆ. ู„ูˆ ู‚ุงู„ูƒ Non-decreasingุŒ ูŠุจู‚ู‰ ู…ู…ูƒู† ูŠูƒูˆู† ููŠู‡ Duplicates. ู…ุชูุชุฑุถุด ุจู†ุงุกู‹ ุนู„ู‰ ุงู„ู€ Sample Input ุฅู† ูƒู„ ุงู„ุชุณุชุงุช ุฒูŠู‡.

Testing your Code

ุจู†ุนู…ู„ ุชุณุช ุนุดุงู† ู†ุฎุชุจุฑ ุงู„ู€:

  • ุงู„ู€ Correctness: ุจูŠุทู„ุน ุงู„ุฅุฌุงุจุฉ ุงู„ู…ุชูˆู‚ุนุฉ.
  • ุงู„ู€ Completeness: ูƒู„ ุงู„ู€ Paths ุจุชุทู„ุน ุฅุฌุงุจุฉ.
  • ุงู„ู€ Limits: ู…ุด ุจู€ Crash ู„ูˆ ุฏุฎู„ุชู„ู‡ Empty Array ุฃูˆ ุงู„ู…ุงูƒุณูŠู…ู… ุจุชุงุน ุงู„ุฑูŠู†ุฌ. (ูˆุฏูŠ ุงุณู…ู‡ุง ุงู„ู€ Minimum/Maximum test cases ุจุฌุงู†ุจ ุงู„ู€ Random/System tests).

The Interview Scenario

ุฏุฎู„ุช ุงู„ุงู†ุชุฑููŠูˆ ูˆุฃุฎุฏุช ุงู„ู…ุณุฃู„ุฉุŒ ู‡ุชุนู…ู„ ุฅูŠู‡ุŸ

  1. ุงู‚ุฑุฃ ุงู„ู…ุณุฃู„ุฉ ููŠ ุณุฑูƒ ูˆุญุงูˆู„ ุชุณุชูˆุนุจู‡ุง. ู…ุชุฏูŠุด ู„ู†ูุณูƒ ุฃูƒุชุฑ ู…ู† ุฏู‚ูŠู‚ุชูŠู† ุตู…ุช.
  2. ู„ูˆ ุฌุงู„ูƒ ุญู„ุŒ ู‚ูˆู„ู‡.
  3. ู„ูˆ ู…ุฌุงู„ูƒุด ุญู„ุŒ ุงุจุฏุฃ ุงุชูƒู„ู… ูˆุงูƒุณุฑ ุงู„ุตู…ุชุŒ ู‚ูˆู„ู‡ โ€œุงู„ู…ุณุฃู„ุฉ ุจุชู‚ูˆู„ ูƒุฐุง ูˆุฃู†ุง ูู‡ู…ุช ูƒุฐุงุŒ ู‡ู„ ุฏู‡ ุตุญุŸโ€
  4. ู„ูˆ ู„ุณู‡ ู…ููŠุด ุญู„ุŒ ุงู„ู€ Safest Side ู‡ูˆ ุงู„ู€ Brute Force. ุฃุถุนู ุงู„ุฅูŠู…ุงู† ุฅู†ูƒ ุชุนุฑู ุชุฌูŠุจู‡.

ูˆู…ู…ูƒู† ูŠู‚ูˆู„ูƒ ู…ุงุชูƒุชุจู‡ูˆุดุŒ ุจุณ ู…ูŠุฒุฉ ุงู„ู€ Brute Force ุฅู†ู‡ ุจูŠูุชุญู„ูƒ ุณูƒูƒ ูˆุฃุจูˆุงุจ ุชู‚ุฏุฑ ุชุฌูŠุจ ู…ู†ู‡ุง OptimizationsุŒ ุจุชุดูˆู ุฅูŠู‡ ุงู„ุฎุทูˆุงุช ุงู„ู€ Redundant (ุงู„ู…ูƒุฑุฑุฉ) ูˆุชุจุฏุฃ ุชุญุณู†ู‡ุง.


Recursion

ุงู„ู€ Recursion ู…ู„ูˆุด ุญู„ ุณุญุฑูŠุŒ ู‡ูˆ ุนุจุงุฑุฉ ุนู† Function ุจุชู†ุงุฏูŠ ู†ูุณู‡ุงุŒ ูˆู„ูŠู‡ุง Base Condition (ุนุดุงู† ุฃุฎุฑุฌ ู…ู†ู‡) ูˆ State ูˆ State Transition. ุนุดุงู† ุชุจู‚ู‰ ูƒูˆูŠุณ ููŠู‡ ู„ุงุฒู… ุชุญู„ ูƒุชูŠุฑ ู„ุญุฏ ู…ุง ูŠุชูƒูˆู† ุนู†ุฏูƒ ู…ู‡ุงุฑุฉ ุงู„ู€ Pattern RecognitionุŒ ูˆุชุจู‚ู‰ ู‚ุงุฏุฑ ุชุฑุจุท ุงู„ู…ุณุฃู„ุฉ ุงู„ุฌุฏูŠุฏุฉ ุจู…ุณุงุฆู„ ุญู„ูŠุชู‡ุง ู‚ุจู„ ูƒุฏู‡ ูˆุชุถูŠู‚ ู…ุณุงุญุฉ ุงู„ุจุญุซ ููŠ ุฏู…ุงุบูƒ. ุฃุบู„ุจ ู…ุณุงุฆู„ ุงู„ู€ Trees, Graphs (DFS), ุงู„ู€ CombinationsุŒ ูˆุงู„ู€ DP ู‚ุงูŠู…ุฉ ุนู„ู‰ ุงู„ู€ Recursion.

Problem Example: Climbing Stairs

ู„ูˆ ุนู†ุฏูƒ ุณู„ู… ุทูˆู„ู‡ ุŒ ูˆููŠ ูƒู„ ุฎุทูˆุฉ ู…ุณู…ูˆุญู„ูƒ ุชุทู„ุน ูŠุง ุณู„ู…ุฉ ูŠุง ุณู„ู…ุชูŠู†ุŒ ููŠ ูƒุงู… ุทุฑูŠู‚ุฉ ุชูˆุตู„ ุจูŠู‡ู… ู…ู† ุฒูŠุฑูˆ ู„ู€ ุŸ ู„ูˆ ุŒ ุนู†ุฏูƒ 3 ุทุฑู‚ (1+1+1ุŒ 1+2ุŒ 2+1). ู„ูˆ ุŒ ุนู†ุฏูƒ 5 ุทุฑู‚. (ู…ุด ุŒ ุงู„ู„ูŠ ู‚ุงู„ ูƒุฏู‡ ุฏู‡ ุชุณุฑุน ูˆู‡ุชุงุฎุฏ ุนู„ูŠู‡ Rejection ููŠ ู†ุต ุฏู…ุงุบูƒ!).

ุงู„ุญู„: ุนู†ุฏ ูƒู„ ุฎุทูˆุฉุŒ ุฅู†ุช ุนู†ุฏูƒ ุงุญุชู…ุงู„ูŠู† (Two Calls): ูŠุง ุชุทู„ุน ุณู„ู…ุฉุŒ ูŠุง ุชุทู„ุน ุณู„ู…ุชูŠู†. ุนุดุงู† ู†ูˆุตู„ ู„ู„ุณู„ู…ุฉ ุงู„ู€ ุŒ ุนุฏุฏ ุงู„ุทุฑู‚ ู‡ูˆ ู…ุฌู…ูˆุน ุนุฏุฏ ุงู„ุทุฑู‚ ุงู„ู„ูŠ ูˆุตู„ุชู†ูŠ ู„ู„ุณู„ู…ุฉ ุงู„ู„ูŠ ู‚ุจู„ูŠ () + ุนุฏุฏ ุงู„ุทุฑู‚ ุงู„ู„ูŠ ูˆุตู„ุชู†ูŠ ู„ู„ุณู„ู…ุฉ ุงู„ู„ูŠ ู‚ุจู„ ู‚ุจู„ูŠ (). ูˆุฏู‡ ุจุงู„ุธุจุท ู†ูุณ ู…ู†ุทู‚ ุงู„ู€ Fibonacci.

graph TD
    A[4] --> B[3]
    A --> C[2]
    B --> D[2]
    B --> E[1]
    C --> F[1]
    C --> G[0]
    D --> H[1]
    D --> I[0]

ุงู„ู€ Recursion Tree ุฏูŠ ุจุชูˆุถุญ ุงู„ู…ุณุงุฑุงุชุŒ ูˆูƒู„ ู…ุง ุจู†ูˆุตู„ ู„ุตูุฑ ุจู†ุฑุฌุน 1ุŒ ูˆุฃูŠ ุญุงุฌุฉ ุบูŠุฑ ุตุงู„ุญุฉ ุจุชุฑุฌุน ุตูุฑ ูˆู†ุจุฏุฃ ู†ุฌู…ุน ูˆุฅุญู†ุง ุทุงู„ุนูŠู†.

ุงู„ู€ Complexity ู‡ู†ุง: ุจู…ุง ุฅู† ุนู†ุฏ ูƒู„ ุฎุทูˆุฉ ููŠ ุงุญุชู…ุงู„ูŠู†ุŒ ูˆุทูˆู„ ุงู„ุณู„ู… ุŒ ูุงู„ู€ Time Complexity ู‡ูŠ (ู…ุด !).

ุทุฑู‚ ุฅุฑุฌุงุน ุงู„ู†ุชูŠุฌุฉ ู…ู† ุงู„ู€ Recursion:

  1. ุชุฌู…ุน ุงู„ู†ุชูŠุฌุฉ ููŠ Variable ูˆุฅู†ุช ู†ุงุฒู„ ูˆุชุจุนุชู‡ ู…ุนุงูƒ ูƒู€ Parameter.
  2. ุชุณุชุฎุฏู… Global Variable (ุฃูˆ Pass by Reference) ุชุฌู…ุน ููŠู‡ ูˆู„ู…ุง ุชุฎู„ุต ูŠูƒูˆู† ุดุงูŠู„ ุงู„ุฅุฌุงุจุฉ ูˆุงู„ู€ Function ุชูƒูˆู† void.
  3. ุงู„ู€ Function ู†ูุณู‡ุง ุชุนู…ู„ Return ูˆุชุจู‚ู‰ ุจุชุนู…ู„ Sum ูˆุฅู†ุช ุทุงู„ุน ู„ููˆู‚ ููŠ ุงู„ู€ Tree.

Brute Force & Optimization

ุงู„ู€ Brute Force ู‡ูˆ ุงู„ุญู„ ุงู„ู€ Naive ุงู„ู„ูŠ ุจูŠู€ Enumerate ุฃูˆ ุจูŠุฌุฑุจ ูƒู„ ุงู„ู€ Search Space ูˆูƒู„ ุงู„ุงุญุชู…ุงู„ุงุช ุจุบุจุงุก ูˆุจุนุฏูŠู† ูŠุชุฃูƒุฏ ุฅูŠู‡ ูŠู†ูุน ูˆุฅูŠู‡ ู„ุฃ.

ุนูŠูˆุจู‡: ุจุทูŠุก ุฌุฏุงู‹ุŒ ุจูŠุณุชู‡ู„ูƒ ุชุงูŠู… ูˆู…ูŠู…ูˆุฑูŠ ูˆุจูŠุฎู„ูŠูƒ ุชุญุณ ุจุงู„ูŠุฃุณ. ู…ู…ูŠุฒุงุชู‡:

  1. ุญู„ ู…ุถู…ูˆู† ูˆุณู‡ู„ ูˆ Complete.
  2. ุจู†ุญุชุงุฌู‡ ุนุดุงู† ู†ุนู…ู„ Validation ู„ู„ู€ Algorithms ุงู„ุฌุฏูŠุฏุฉ ุงู„ู„ูŠ ุจู†ูƒุชุจู‡ุง (ู†ู‚ุงุฑู† ุงู„ุฅุฌุงุจุงุช ุจุจุนุถ).
  3. ุจูŠูุชุญ ุนูŠู†ูƒ ู„ู„ู€ Optimizations ูˆูŠูˆุถุญู„ูƒ ุฅู†ุช ุจุชุนู…ู„ ุฅูŠู‡ ู…ู„ูˆุด ู„ุงุฒู…ุฉ ูุชุจุฏุฃ ุชุญุณู†ู‡.

ู…ุซุงู„ ู„ู„ุชูˆุถูŠุญ: ุนุงูŠุฒูŠู† ู†ุฌูŠุจ ุฃูƒุจุฑ ู…ุฌู…ูˆุน ู„ุฑู‚ู…ูŠู† (Max Sum) ููŠ Array.

  • ุงู„ุญู„ ุงู„ุจุฑูˆูˆุช ููˆุฑุณ: ู‡ู†ุฌุฑุจ ูƒู„ ุฑู‚ู…ูŠู† ู…ุน ุจุนุถ (All Pairs) ูˆู†ุดูˆู ุงู„ู…ุฌู…ูˆุนุŒ ูˆุฏู‡ ุจูŠุงุฎุฏ . ุจุณ ู‡ู„ ู…ู†ุทู‚ูŠ ุฃุฌู…ุน ุฃุฑู‚ุงู… ุตุบูŠุฑุฉ ูˆุฃู†ุง ุนู†ุฏูŠ ุฃุฑู‚ุงู… ุจู€ 10,000 ููŠ ุงู„ู€ ArrayุŸ
  • ุงู„ุชุญุณูŠู† ุงู„ุฃูˆู„: ุนุดุงู† ู†ุชุฌุงู‡ู„ ุงู„ุฃุฑู‚ุงู… ุงู„ุตุบูŠุฑุฉุŒ ู‡ู†ุฏูˆุฑ ุนู„ู‰ ุงู„ู…ุงูƒุณูŠู…ู…ุŒ ูˆุจุนุฏูŠู† ู†ุนู…ู„ Loop ุชุงู†ูŠุฉ ู†ุฏูˆุฑ ุนู„ู‰ ุชุงู†ูŠ ุฃูƒุจุฑ ุฑู‚ู…. ูƒุฏู‡ ุงู„ู€ Complexity ุจู‚ุช .
  • ุงู„ุชุญุณูŠู† ุงู„ุชุงู†ูŠ: ู„ูŠู‡ ู†ู„ู ู…ุฑุชูŠู†ุŸ ู†ุนู…ู„ Loop ูˆุงุญุฏุฉุŒ ูˆู†ุญุชูุธ ุจู…ุชุบูŠุฑูŠู† (Max 1 ูˆ Max 2)ุŒ ูˆุทูˆู„ ู…ุง ุฅุญู†ุง ู…ุงุดูŠูŠู† ู†ู‚ุงุฑู† ุจูŠู‡ู… ูˆู†ุญุฏุซู‡ู…. ูƒุฏู‡ ุญู„ูŠู†ุง ุงู„ู…ุณุฃู„ุฉ ููŠ ุนู† ุทุฑูŠู‚ ุฅู†ู†ุง ุญู„ู„ู†ุง ุงู„ู€ Brute force ูˆู„ู‚ูŠู†ุง Optimization.

Corrections & Enhancements

  • ุงู„ู€ Correction: ุงู„ู€ int ููŠ ู…ุนุธู… ุงู„ุฃู†ุธู…ุฉ ุงู„ุญุฏูŠุซุฉ ุญุฌู…ู‡ 4 BytesุŒ ูˆุจุงู„ุชุงู„ูŠ ุจูŠุญุฌุฒ 4 ุฎู„ุงูŠุง (Cells) ู…ุชุชุงู„ูŠุฉ ููŠ ุงู„ู€ Memory ูˆู„ูŠุณ ุฎู„ูŠุฉ ูˆุงุญุฏุฉ.
  • ุงู„ู€ Advanced Topics: ููŠ ู…ุณุฃู„ุฉ ุงู„ู€ Climbing StairsุŒ ุงู„ุญู„ ุงู„ู€ Recursive ุงู„ุจุณูŠุท ุจูŠุคุฏูŠ ุฅู„ู‰ Time Complexity ุจุณุจุจ ุงู„ู€ Overlapping Subproblems (ุชูƒุฑุงุฑ ุญุณุงุจ ู†ูุณ ุงู„ู‚ูŠู… ุนุฏุฉ ู…ุฑุงุช ููŠ ุดุฌุฑุฉ ุงู„ุงุณุชุฏุนุงุก). ุงู„ุทุฑูŠู‚ุฉ ุงู„ู…ุชู‚ุฏู…ุฉ ูˆุงู„ูุนุงู„ุฉ ู„ุญู„ ู‡ุฐู‡ ุงู„ู…ุดูƒู„ุฉ ู‡ูŠ ุงุณุชุฎุฏุงู… ุงู„ู€ Dynamic Programming (Memoization) ุนู† ุทุฑูŠู‚ ุญูุธ ุงู„ู†ูˆุงุชุฌ ุงู„ุชูŠ ุชู… ุญุณุงุจู‡ุง ููŠ ู…ุตููˆูุฉ (Array) ู„ุชุฌู†ุจ ุฅุนุงุฏุฉ ุญุณุงุจู‡ุงุŒ ู…ู…ุง ูŠู‚ู„ู„ ุงู„ู€ Time Complexity ุฅู„ู‰ .