हर AI system को डेटा स्टोर और तेज़ी से एक्सेस करना होता है। चाहे एक image के pixel values हों, 50,000 शब्दों की vocabulary हो, या लाखों users की preferences - सही data structure चुनना तय करता है कि AI कितनी तेज़ी से काम करेगा।
दो structures सबसे ज़्यादा इस्तेमाल होते हैं: arrays और hash maps। इन्हें समझ लिया तो AI pipeline की नींव पक्की।
Array एक नंबर वाली लिस्ट है जहाँ items मेमोरी में एक साथ रखे जाते हैं। हर item का एक index होता है - जो zero से शुरू होता है।
index: 0 1 2 3 4
value: ["cat", "dog", "bird", "fish", "frog"]
चूँकि items साथ-साथ बैठे हैं, किसी भी position पर सीधे पहुँच सकते हैं। Item 3 चाहिए? बस - कोई searching नहीं। यही O(1) access है - 10 items हों या 10 million, समय बराबर लगता है।
बीच में item insert या delete करना महंगा है - बाद के सारे items को शिफ्ट करना पड़ता है। यह O(n) है - जितने ज़्यादा items, उतना ज़्यादा समय।
अगर 10,000 गानों की playlist में position 5 पर नया गाना डालना हो, तो position 5 के बाद के हर गाने को शिफ्ट करना पड़ेगा। कोई streaming service इसे बिना slow हुए कैसे handle करेगी?
Hash map (जिसे dictionary या hash table भी कहते हैं) डेटा को key-value pairs में स्टोर करता है। Index number की जगह एक meaningful key से access करते हैं।
word_counts = {
"hello": 42,
"world": 37,
"AI": 156
}
"AI" का count चाहिए? Hash map एक hash function से key को internally index में बदलता है। नतीजा? औसतन lookup - arrays जैसा, बस numbers की जगह names से।
Sign in to join the discussion
Python की dictionaries hash maps हैं। जब ChatGPT training के दौरान word frequencies गिनता है, तो hash-map जैसी structures से पूरे internet के text में अरबों शब्दों को track करता है।
| Operation | Array | Hash Map | |-----------|-------|----------| | Index से access | O(1) ⚡ | N/A | | Key से access | O(n) 🐢 | O(1) ⚡ | | End में insert | O(1) ⚡ | O(1) ⚡ | | बीच में insert | O(n) 🐢 | N/A | | Value ढूँढना | O(n) 🐢 | O(1) ⚡ |
O(1) का मतलब "साइज़ कितना भी हो, instant" और O(n) का मतलब "डेटा बड़ा तो slow।"
आपके पास 100,000 user profiles हैं और username से user ढूँढना है। कौन सा structure सबसे तेज़ होगा?
Interviews और AI दोनों में सबसे उपयोगी patterns में से एक - occurrences गिनना:
counts = {}
for each word in text:
if word in counts:
counts[word] = counts[word] + 1
else:
counts[word] = 1
Text का एक ही pass - और हर शब्द की frequency मिल गई। Language models भी बड़े scale पर यही approach इस्तेमाल करते हैं।
एक array of numbers और एक target दिया है - दो numbers ढूँढो जिनका जोड़ target बने। Naive तरीका हर pair check करता है - O(n²)। Smart तरीका hash map इस्तेमाल करता है:
seen = {}
for each number in array:
complement = target - number
if complement in seen:
return [seen[complement], current_index]
seen[number] = current_index
एक pass, O(n) time। Hash map याद रखता है कि पहले क्या-क्या देखा है।
Two-sum में hash map वाला approach हर pair check करने से तेज़ क्यों है?
एक recommendation engine को suggest करने से पहले check करना है कि user ने film पहले देखी है या नहीं। Watch history array में रखोगे या hash map में? क्या trade-offs हो सकते हैं?
AI model word embeddings को 300 numbers के arrays में स्टोर करता है। Arrays यहाँ अच्छा choice क्यों हैं?