ููุชุนูู ู ุน ุจุนุถ ุฅูู ุงูู ุณุงุฆู ุงููู ุจุชูุฌู ูู ุงูู Interviewsุ ุฅุฒุงู ูููุฑ ูููุง ููุญููุงุ ูุฅุฒุงู ูุนู ู Analysis ููู Complexity
ุฃุบูุจ ุงูููุช ูููู ุจุชููู ู ู ูุฌูุฉ ูุธุฑ ุงูู Concepts ูุงูู Algorithms ูู ุด ููุชู ุฃูู ุจุงูู Implementation. ูุงุฏุฑุงู ูู ุง ูุชูุงูููู ุญุงุทุท ููุฏ ูุจูุชูุงูุด ูููุ ุจุณ ูู ุง ุฃุนู ู ูุฏู ูุณุชุฎุฏู C++.
ุฏูุฑู ู ู ุงููุญุธุฉ ุฏู ุจูุชูุฎุต ูู ุญุงุฌุชูู ู ุงููู ุด ุชุงูุช:
- ุชุงุฎุฏ ุงููู ููุดุฑุญู (ุฒู ุงูู BFS ุฃู ุงูู DFS ุฃู ุงูู Graph Traversal) ูุชุฑูุญ ุชุดูู ุงูู Implementation ุจุชุงุนู ูุชุฐุงูุฑู. ุชุญุงูู ุชุนู ูู ุจููุณู (ูุบุงูุจุงู ูุชูุดู ูู ุงูุฃูู)ุ ูุจุนุฏูู ุชุดููู ูุชุฐุงูุฑู.
- ุงูุญู ุซู ุงูุญู ุซู ุงูุญู! ุฃูุง ุจุดูู ุนูู ุงููุงุณ ุงููู ุจุชุณุงูุฑ ู ู ููุฑ ุงูุดูุฎ ูุบูุฑูุง ุนุดุงู ุชุญุถุฑ ุงูู 3 ุณุงุนุงุชุ ูุฃู ูู ุงููู ุจูุนู ูู ุฏู ุจูู ุซู 20-30% ุจุณ ู ู ุงูู ุฌููุฏ ุงูู ุทููุจ ุนุดุงู ุชุนุฏู ู ู ุงูู Interviews. ุงูู 70-80% ุงูุจุงูููู ูุงูู ูู ุนูู ุฅูู ุชุญู ูู ููู . ูู ุญููุช ุดูุฑูู ูุจุนุฏูู ูุทุนุช 6 ุดููุฑุ ูุฃูู ู ุนู ูุชุด ุญุงุฌุฉ.
Course Content Overview
ุงูููุฑุณ ุจุชุงุนูุง:
- ุงูููุงุฑุฏุฉ: Introduction ููู C++ุ ุงูู STLุ ุงูู Recursionุ ุงูู Brute Forceุ ูุดููุฉ Problem Solving Basics.
- ุงูุณูุดู ุงูุฌุงูุฉ: ุงูู Complexity.
- ุงูุณูุดู ุงูุชุงูุชุฉ: ุงูู Sorting ูุงูู Hashing.
- ุงูุณูุดู ุงูุฑุงุจุนุฉ: ุงูู Trees.
- ุงูุณูุดู ุงูุฎุงู ุณุฉ: ุงูู Graphs.
- ุงูุณูุดู ุงูุณุงุฏุณุฉ: ุงูู Dynamic Programming.
- ุงูุณูุดู ุงูุณุงุจุนุฉ: ุงูู 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:
-
Pass by Value: ุจุชุงุฎุฏ ูุณุฎุฉ (Copy) ู ู ุงูุฏุงุชุง ูุชุญุทูุง ูู Variable ุฌุฏูุฏ ูุชุจุนุชู ููู Function. ูู ุบูุฑุช ููู ุชู ุฌููุ ุงูููู ุฉ ุงูุฃุตููุฉ ุจุฑู ุนู ุฑูุง ู ุง ูุชุชุบูุฑ.
- ุงูู ูุฒุฉ: ุงูููู ุฉ ุงูุฃุตููุฉ ู ุญููุธุฉ.
- ุงูุนูุจ: ุจูุณุชููู ู ูู ูุฑู ูุชูุฑ ููู ุงู ููุช ( Time)ุ ุชุฎูู ูู ุจุชุจุนุช Array ูุจูุฑ ุฌุฏุงูุ ูุชุถูุน ููุช ูู ูุณุฎ ูู Element ููู ุงู ูุชุณุชููู ู ูู ูุฑู ุฅุถุงููุฉ.
-
Pass by Reference: ุจูุณุชุฎุฏู ุงูู Reference Variable (ุจุนูุงู ุฉ
&ุฒูint& x). ุงูู Variable ุฏู ุจูุดุชุฑู ู ุน ุงูู Variable ุงูุฃุตูู ูู ุงูู Symbol Table ุจูุดุงูุฑูุง ุนูู ููุณ ุงูุนููุงู. ู ููุด ุญุงุฌุฉ ุงุณู ูุง ุฅูู ุชุจุนุช ุงูู Variable ููุณู ูู C++ุ ุฅูุช ุจุชุจุนุช ุฑููุฑูุณ ููู.- ุงูู ูุฒุฉ: ู ููุด ุฃู ุงุณุชููุงู ููููุช ุฃู ุงูู ูู ูุฑู.
- ุงูุนูุจ: ูู ุบูุฑุช ุฃู ุญุงุฌุฉ ุฌูู ุงูู Functionุ ูุชุชุบูุฑ ุจุฑู ูู ุงูููู ุฉ ุงูุฃุตููุฉ.
-
Pass by Pointer: ุจุฏู ู ุง ุชุจุนุช Referenceุ ุฅูุช ุจุชุจุนุช ุงูู Address ุจุชุงุน ุงูู Variable (ุฒู
int* x). ููู ุง ุชูุฌู ุชูุงุฏู ุงูู Function ุจุชุจุนุช&x. ุฌูู ุงูู Function ุจุชุนู ู Dereference ุนุดุงู ุชุบูุฑ ุงูุฏุงุชุง. ูู ุจูุนู ู ููุณ ูุธููุฉ ุงูู Reference ุจุงูุธุจุท ุจุณ ุงูู ููุงููุฒู ูุงูู Syntax ู ุฎุชูู. -
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() ุฅุญูุง ุนูุฏูุง:
- Cs IEnumerable
- Cs IEnumerator
- ู ุงูุฃุดูุฑ ูุงูุฃุณูู:
foreach
ูู 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_boundBuilt-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.
- ุงูู Primitive Variables (ุฒู
ู ุซุงู 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
| Feature | Stack | Heap |
|---|---|---|
| Size | Limited | Large |
| Management | Automatic | Manual (C++) / GC (C#) |
| Allocation Time | Compile Time (mostly) | Run Time |
| Used For | Local variables, Call stack | Objects, Dynamic structures |
| Resize | No | Yes |
- ุงูู 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
ุฏุฎูุช ุงูุงูุชุฑููู ูุฃุฎุฏุช ุงูู ุณุฃูุฉุ ูุชุนู ู ุฅููุ
- ุงูุฑุฃ ุงูู ุณุฃูุฉ ูู ุณุฑู ูุญุงูู ุชุณุชูุนุจูุง. ู ุชุฏูุด ูููุณู ุฃูุชุฑ ู ู ุฏูููุชูู ุตู ุช.
- ูู ุฌุงูู ุญูุ ูููู.
- ูู ู ุฌุงููุด ุญูุ ุงุจุฏุฃ ุงุชููู ูุงูุณุฑ ุงูุตู ุชุ ูููู โุงูู ุณุฃูุฉ ุจุชููู ูุฐุง ูุฃูุง ููู ุช ูุฐุงุ ูู ุฏู ุตุญุโ
- ูู ูุณู ู ููุด ุญูุ ุงูู 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:
- ุชุฌู ุน ุงููุชูุฌุฉ ูู Variable ูุฅูุช ูุงุฒู ูุชุจุนุชู ู ุนุงู ูู Parameter.
- ุชุณุชุฎุฏู
Global Variable (ุฃู Pass by Reference) ุชุฌู
ุน ููู ููู
ุง ุชุฎูุต ูููู ุดุงูู ุงูุฅุฌุงุจุฉ ูุงูู Function ุชููู
void. - ุงูู Function ููุณูุง ุชุนู ู Return ูุชุจูู ุจุชุนู ู Sum ูุฅูุช ุทุงูุน ูููู ูู ุงูู Tree.
Brute Force & Optimization
ุงูู Brute Force ูู ุงูุญู ุงูู Naive ุงููู ุจูู Enumerate ุฃู ุจูุฌุฑุจ ูู ุงูู Search Space ููู ุงูุงุญุชู ุงูุงุช ุจุบุจุงุก ูุจุนุฏูู ูุชุฃูุฏ ุฅูู ูููุน ูุฅูู ูุฃ.
ุนููุจู: ุจุทูุก ุฌุฏุงูุ ุจูุณุชููู ุชุงูู ูู ูู ูุฑู ูุจูุฎููู ุชุญุณ ุจุงููุฃุณ. ู ู ูุฒุงุชู:
- ุญู ู ุถู ูู ูุณูู ู Complete.
- ุจูุญุชุงุฌู ุนุดุงู ูุนู ู Validation ููู Algorithms ุงูุฌุฏูุฏุฉ ุงููู ุจููุชุจูุง (ููุงุฑู ุงูุฅุฌุงุจุงุช ุจุจุนุถ).
- ุจููุชุญ ุนููู ููู 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 ุฅูู .