Data Structures · ডেটা স্ট্রাকচার
How data is organised in memory — arrays, lists, trees, graphs, hashing — and the cost of each operation.
- Past MCQs
- 110
- Written
- 108
- Chapters
- 7
How Data Structures is asked · পরীক্ষায় যেভাবে আসে
Our 109 papers that ask Data Structures (2015–2026) hold 218 of its questions; Trees alone makes up 29.4% of them. It makes up 6.8% of the questions in our M.Sc. admission (CSE and ICT) papers, against 2.3% in ICT teaching posts (NTRCA). 8 of its MCQs came back in a second exam.
- Papers that ask it
- 109
- Years covered
- 2015–2026
- MCQ / written
- 110 / 108
- MCQs asked again
- 8
Most asked chapters · বেশি আসা অধ্যায়
Each chapter's share of the 218 past-exam questions
- Trees29.4%
- Stacks & Queues28.4%
- Arrays & Linked Lists24.8%
- Graphs5.0%
- Hashing4.1%
- Complexity0.9%
Share of each exam's questions · পরীক্ষাভিত্তিক অংশ
Data Structures questions in each exam's papers, and their share of that exam's questions
- M.Sc. admission (CSE and ICT) of 177126.8%
- BCS (ICT and CSE cadre) of 492193.9%
- Bank IT jobs of 2,388863.6%
- BPSC non-cadre IT posts of 716253.5%
- Government IT officer posts of 2,337682.9%
- Defence organisations' IT posts of 3712.7%
- ICT teaching posts (NTRCA) of 30072.3%
Asked in more than one exam · একাধিক পরীক্ষায় এসেছে
8 MCQs in Data Structures came back word for word in a second exam; the 3 most repeated technical ones below, each linked to the papers.
The data structure required to check whether an expression contains balanced parentheses is?
Data Structures, asked in 2 exams:Combined Bank Officer (IT) Jan 2026Combined Bank Assistant Programmer 2018
What is the postfix expression of the string a+(b-c)*d?
Data Structures, asked in 2 exams:Sonali Bank Senior Officer Oct 2021Sonali Bank Assistant Database Administrator 2020
Suppose you want to insert n elements into an empty linked list while maintaining the sorted order. What is the worst-case time complexity?
Data Structures, asked in 2 exams:Bank Assistant Programmer Mar 2021Janata Bank Assistant Network Engineer 2020
Counted from our own past papers: every past-exam MCQ and written question, each filed under one syllabus chapter. Practice questions and questions held for live tests are not counted; no answer is shown here.
Syllabus by chapter · অধ্যায়ভিত্তিক সিলেবাস
- Arrays & Linked Lists · 157 questions
- Array Representation of Binary Tree, O(1) enQueue/deQueue using a singly linked list, BFS vs DFS; Linked List vs Array, Constructing a Binary Tree from Preorder and Postorder Traversal, Types of Linked List, Singly Linked List vs Doubly Linked List
- Stacks & Queues · 180 questions
- Balanced Parentheses Checking, Stack vs Queue, Infix to Postfix Conversion and Evaluation, Binary Max-Heap Insertion, Tracing Push and Pop Operations on a Stack, Max-Heap Properties & Heap Sort
- Trees · 220 questions
- Minimum and maximum nodes in a binary tree, Infix to prefix conversion, BST Insertion, Postorder Traversal, and Deletion, Binary Tree Traversal (Inorder/Preorder/Postorder), Proper (Full) Binary Tree - Leaf/Internal Node Relation, Adjacency List vs. Adjacency Matrix for a Complete Binary Tree
- Graphs · 38 questions
- Adjacency List vs Adjacency Matrix Efficiency, True/False statements on Graphs, Eulerian and Hamiltonian Graphs, Adjacency Matrix vs Adjacency List
- Hashing · 38 questions
- Linear Probing Collision Resolution, Separate Chaining Collision Resolution, Hash Table — O(1) Average Search, Quadratic Probing Collision Resolution, O(1) average-case search using a hash table
- Complexity · 19 questions
- Linear vs Non-linear Data Structures; logarithmic-complexity structure
- Mixed Topics · 64 questions
- Where recursion is used in data structures, Linear বনাম Non-Linear Data Structure, Infix, Prefix, Postfix Notation, Operations on Data Structures; Prefix/Infix/Postfix, Linear vs Non-linear Data Structures, Linear vs Non-Linear Data Structures
Loading sample questions…
Start learning: Complexity Analysis and Big-O Notation · কমপ্লেক্সিটি অ্যানালাইসিস ও Big-O নোটেশন
Complexity Analysis and Big-O Notation
Open the contacts app on your phone and type the first letters of a friend's name. The name pops up before you finish typing. Now picture the Election Commission's voter list: twelve crore names, kept in alphabetical order. Someone types a name and the answer must still come back at once. Your first guess might be that it needs a much faster computer. It doesn't. It needs a smarter way of looking. If the program checked the names one by one from the top, it could end up making twelve crore comparisons. A program that opens the list in the middle, throws away the half that cannot hold the name, and repeats, never needs more than 27.
The same two programs, written by two students, would both pass a test on a hundred names. They part ways only when the data grows. This chapter is about seeing that difference coming before you run anything, and about the small language we use to say it: time complexity, space complexity, and Big-O notation. Every later chapter of Data Structures leans on it, because every structure you will meet (array, list, stack, tree, hash table) is judged by what its operations cost.
By the end of this chapter you will be able to count the work a piece of code does and write its Big-O with the reason. You will define O, Ω and Θ properly, prove a bound with constants, and rank the usual growth classes from memory. You will find the call-stack space of a recursive function, write the recurrence of a divide-and-conquer algorithm and solve it with the Master Theorem, explain why quicksort can be slow, and show why a dynamic array's push is cheap on average even though one push can be expensive.
We go in six parts, from easy to hard: what we measure and the tools we need (logarithms and two sums); the notations and the ladder of growth rates; reading Big-O off loops; space and the call stack; recurrences and the Master Theorem; and finally amortised cost.
:::why ids=126-13,074-37,650-7,659-21,656-76,062-36,137-3,133-22,022-3,wo00386,wo00628,wo00337,wo00356,wo00940,wo01421,wo02190,wo02424,wo02540
Exams ask this chapter in two ways. The one-mark MCQs test a single fact: the two measures of an algorithm, what Θ means, which of four classes runs fastest, where a recursion's space comes from, what binary search or merge sort costs. The written papers ask you to work something out: find the time and space complexity of a loop (Bangladesh Bank, 2016; DPDC, 2025), explain what Big-O and Big-Omega are, derive the recurrence of merge sort or binary search, or solve a recurrence such as T(n) = 3T(n − 1) + 2. Each of these is taught below with its reason, and the numbers in it come from programs that were really run.
:::
:::hook from=062-36
Answer: (c) O(log n). Each comparison throws away half of what is left of the sorted array, and n can be halved only about log₂ n times. For a sorted array of 1,000,000 numbers a real run needed 18.95 comparisons on average (and 20 at most), which is log₂ n minus a little. The traps: O(1) is only the best case (the key happens to sit in the very first middle cell); O(n log n) is the cost of sorting (merge sort), not of one search; and O(n/2 log n) is not even a proper class, because Big-O drops the constant ½. Part 1 builds this halving idea from scratch.
:::
Part 1: What we measure, and the two tools we need
Three words that get mixed up
Think of a restaurant. The menu promises what you can order: a plate of rice, a bowl of dal, a fish curry. It says nothing about how the kitchen is laid out. The kitchen, with its stoves, shelves and fridges, is how the promise is kept. And the recipe is the list of steps the cook follows for one dish. Menu, kitchen and recipe are three different things, and in Data Structures they have names.
An ADT (Abstract Data Type) is the menu: it lists the operations and the rule they obey, and it says nothing about memory. A data structure is the kitchen: the actual layout of the data in memory that makes those operations work. An algorithm is the recipe: the finite list of steps that solves one problem, such as pushing onto a stack or searching a list.
:::table caption="Three words, one example: the stack" wrap
| Term | What it answers | In the stack example |
|---|---|---|
| ADT (Abstract Data Type) | which operations, under which rule: "what it does" | push, pop, top, with the rule LIFO (Last-In-First-Out) |
| Data structure | how the data is laid out in memory: "how it is stored" | an array plus a top index, or a linked list |
| Algorithm | the steps that do one job | push: move top up by one, then store the value there |
| ::: |
Why does a student of complexity need this? Because one ADT can be built on different data structures, and the choice changes the cost. Take the list ADT, "give me the i-th item". In an array the computer jumps straight to the cell, one step whatever i is. In a linked list it must walk from the first node, i steps. Same menu, different kitchen, very different bill. Complexity analysis is how we read the bill before we order.
In one line: an ADT says what, a data structure says how it is stored, an algorithm says the steps; complexity is the price tag on the steps.
Why seconds are the wrong ruler
Two students write a sorting program. Rafi's runs in 3 seconds on his gaming desktop. Mitu's takes 5 seconds on her old laptop. Whose algorithm is better? You can't say. The machine is different, so is the compiler, so is the language, and a video call may be running in the background on one of them. Seconds measure the whole situation, not the idea.
So we throw all of that away and keep the one thing that belongs to the algorithm itself: how the work grows when the input grows. We call the input size n (the number of names, of numbers, of nodes). We call the work T(n), counted in basic operations: one comparison, one addition, one assignment, one array access. A basic operation takes the same small amount of time whatever n is, so counting them gives a number that does not depend on the machine.
What matters most is the shape of T(n), not its exact value. If one program needs 2n steps and another needs 100n, both double their work when n doubles. Both are "linear". Next to the difference between n steps and n² steps, the difference between 2 and 100 is small, and that is why we will soon learn to ignore it.
In one line: we do not time programs; we count basic operations as a function T(n) of the input size n.
Counting one search, and the three cases
Here is the simplest search there is. It looks at the array from the first cell, one by one, and steps counts the comparisons. Every program in this chapter was compiled with gcc -Wall -Wextra and run, and every number shown is the real output.
#include <stdio.h>
int steps; /* counts comparisons */
int linear_search(const int a[], int n, int key) {
for (int i = 0; i < n; i++) {
steps++;
if (a[i] == key) return i;
}
return -1;
}
int main(void) {
int a[10] = {7, 3, 9, 1, 5, 8, 2, 6, 4, 0};
int keys[3] = {7, 0, 11}; /* first, last, absent */
for (int k = 0; k < 3; k++) {
steps = 0;
int pos = linear_search(a, 10, keys[k]);
printf("key %2d: index %2d, comparisons %d\n", keys[k], pos, steps);
}
long total = 0; /* average over every successful search */
for (int k = 0; k < 10; k++) {
steps = 0;
linear_search(a, 10, a[k]);
total += steps;
}
printf("average (found): %.1f\n", total / 10.0);
return 0;
}
key 7: index 0, comparisons 1
key 0: index 9, comparisons 10
key 11: index -1, comparisons 10
average (found): 5.5
The array size is the same, n = 10, yet the work is 1 comparison for one key and 10 for another. So for one n there is not one running time but a range, and each end of the range has a name. The best case is the luckiest input: the key is in the first cell, 1 comparison. The worst case is the unluckiest: the key is in the last cell or absent, n comparisons. The average case assumes every position is equally likely: the successful search then costs 1, 2, …, n comparisons with equal chance, which averages to (n + 1)/2, here 5.5, and it still grows in step with n.
When a question says "the complexity of X" and names no case, it means the worst case. The reason is practical: the worst case is a promise. If it holds, the program can never be slower than that.
In one line: best, average and worst case describe which input of size n we take; with no case named, take the worst.
Time and space: the two measures
An algorithm needs two things from the machine: steps and memory. Time complexity is T(n), the number of basic operations. Space complexity is the memory it needs. Because the input has to be stored anyway, we usually count only the extra (auxiliary) memory: the variables, the new arrays, and, as Part 4 shows, the recursion stack. The linear search above uses i, key and n and nothing more, however long the array is, so its extra space is constant. Exams state the same idea as S(P) = C + S_P(n): a fixed part C (the code's constants and simple variables) plus a part S_P(n) that grows with the input. We keep the growing part.
:::table caption="The two measures of an algorithm" wrap
| Measure | What is counted | Written as |
|---|---|---|
| Time complexity | basic operations (comparisons, additions, assignments) as n grows | T(n) |
| Space complexity | extra memory (variables, arrays, call-stack frames) as n grows | S(n) |
| ::: |
Logarithms: how many times can you halve?
Think of the guessing game. I pick a number between 1 and 1,000 and you may ask only "is it bigger than this?". Ask about 500 first. Whatever I answer, you have cut the choices to half. Ask the middle of what is left, and so on. How many questions do you need? Ten, because 2¹⁰ = 1,024 is just above 1,000. That "how many times can I halve?" is exactly what a logarithm counts.
:::figure caption="Halving 16 cells again and again: four halvings reach a single cell" alt="Five bars of shrinking length: 16 cells at the start, then 8, 4, 2 and finally 1 cell after four halvings." maxw=440
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 176" role="img"><text x="4" y="22" text-anchor="start" font-size="12" class="f-mute">start</text><rect x="86" y="8" width="200" height="20" rx="3" class="f-net"/><text x="292" y="23" text-anchor="start" font-size="12" class="f-ink">16 cells</text><text x="4" y="52" text-anchor="start" font-size="12" class="f-mute">after 1</text><rect x="86" y="38" width="100" height="20" rx="3" class="f-net"/><text x="192" y="53" text-anchor="start" font-size="12" class="f-ink">8 cells</text><text x="4" y="82" text-anchor="start" font-size="12" class="f-mute">after 2</text><rect x="86" y="68" width="50" height="20" rx="3" class="f-net"/><text x="142" y="83" text-anchor="start" font-size="12" class="f-ink">4 cells</text><text x="4" y="112" text-anchor="start" font-size="12" class="f-mute">after 3</text><rect x="86" y="98" width="25" height="20" rx="3" class="f-net"/><text x="117" y="113" text-anchor="start" font-size="12" class="f-ink">2 cells</text><text x="4" y="142" text-anchor="start" font-size="12" class="f-mute">after 4</text><rect x="86" y="128" width="12.5" height="20" rx="3" class="f-net"/><text x="104.5" y="143" text-anchor="start" font-size="12" class="f-ink">1 cell</text><text x="4" y="168" text-anchor="start" font-size="11.5" class="f-mute">Four halvings take 16 cells down to 1, and log₂ 16 = 4.</text></svg>
:::
In words: log₂ n is the power to which 2 must be raised to get n. If 2ᵏ = n then k = log₂ n. Seen from the other side, it is the number of times n can be halved before only one thing is left. When n is not a power of 2 we round: halving n gives ⌊log₂ n⌋ halvings, and a search that tests one cell per halving, plus the final one, takes at most ⌊log₂ n⌋ + 1 comparisons.
:::table caption="Powers of 2 and their logarithms (computed)"
| n | as a power of 2 | log₂ n |
|---|---|---|
| 16 | 2⁴ | 4 |
| 1,000 | about 2^9.97 | 9.97 |
| 1,024 | 2¹⁰ | 10 |
| 1,000,000 | about 2^19.93 | 19.93 |
| 120,000,000 | about 2^26.84 | 26.84 |
| ::: |
That last row is the voter list from the start: log₂ of twelve crore is 26.84, so at most 27 comparisons. A handy memory aid is 2¹⁰ ≈ 1,000: every time the data grows a thousand times, log₂ grows by only about 10. A thousand names need 10 halvings, a million need 20, a billion need 30.
Does the base matter? Only by a constant. log₂ n = 3.32 × log₁₀ n (check: log₂ 10 = 3.3219), and since constants will be thrown away in Part 2, O(log₂ n), O(log₁₀ n) and O(ln n) are the same class, written O(log n).
Now watch halving at work. Below, binary search looks for 61 in a sorted array of 8 numbers. Step through it and watch the range lo…hi shrink: 8 cells, then 4, then 2.
:::widget name=trace lang=c predict=1 title="Binary search: the range halves at every step"
#include <stdio.h>
int main(void) {
int a[8] = {3, 8, 15, 23, 42, 57, 61, 78};
int key = 61, lo = 0, hi = 7, steps = 0;
while (lo <= hi) {
int mid = (lo + hi) / 2;
steps++;
if (a[mid] == key) { printf("found at %d in %d steps\n", mid, steps); return 0; }
if (a[mid] < key) lo = mid + 1;
else hi = mid - 1;
}
printf("not found after %d steps\n", steps);
return 0;
}
4 | a={3,8,15,23,42,57,61,78} | | A sorted array: binary search needs that.
5 | a={3,8,15,23,42,57,61,78}; key=61; lo=0; hi=7; steps=0 | | The search range is the whole array: indices 0 to 7, 8 cells.
6 | a={3,8,15,23,42,57,61,78}; key=61; lo=0; hi=7; steps=0 | | 0 <= 7 is true, so we enter the loop.
7 | a={3,8,15,23,42,57,61,78}; key=61; lo=0; hi=7; steps=0; mid=3 | | Middle cell: (0 + 7) / 2 = 3.
8 | a={3,8,15,23,42,57,61,78}; key=61; lo=0; hi=7; steps=1; mid=3 | | First comparison.
9 | a={3,8,15,23,42,57,61,78}; key=61; lo=0; hi=7; steps=1; mid=3 | | a[3] = 23, not 61.
10 | a={3,8,15,23,42,57,61,78}; key=61; lo=4; hi=7; steps=1; mid=3 | | 23 < 61, so the left half goes. The range is now 4 cells (4 to 7).
6 | a={3,8,15,23,42,57,61,78}; key=61; lo=4; hi=7; steps=1 | | 4 <= 7 is true.
7 | a={3,8,15,23,42,57,61,78}; key=61; lo=4; hi=7; steps=1; mid=5 | | Middle cell: (4 + 7) / 2 = 5.
8 | a={3,8,15,23,42,57,61,78}; key=61; lo=4; hi=7; steps=2; mid=5 | | Second comparison.
9 | a={3,8,15,23,42,57,61,78}; key=61; lo=4; hi=7; steps=2; mid=5 | | a[5] = 57, not 61.
10 | a={3,8,15,23,42,57,61,78}; key=61; lo=6; hi=7; steps=2; mid=5 | | 57 < 61: the left part goes again. The range is 2 cells (6 and 7).
6 | a={3,8,15,23,42,57,61,78}; key=61; lo=6; hi=7; steps=2 | | 6 <= 7 is true.
7 | a={3,8,15,23,42,57,61,78}; key=61; lo=6; hi=7; steps=2; mid=6 | | Middle cell: (6 + 7) / 2 = 6.
8 | a={3,8,15,23,42,57,61,78}; key=61; lo=6; hi=7; steps=3; mid=6 | | Third comparison.
9 | a={3,8,15,23,42,57,61,78}; key=61; lo=6; hi=7; steps=3; mid=6 | found at 6 in 3 steps\n | Found it. 8 cells, then 4, then 2: three steps, which is log₂ 8.
:::
We did not stop at one example. A second program ran binary search for every key in sorted arrays of four sizes and recorded the worst case and the average. The worst case matches ⌊log₂ n⌋ + 1 every time:
n= 8 worst (found) 4 average (found) 2.62
n= 16 worst (found) 5 average (found) 3.38
n= 1000 worst (found) 10 average (found) 8.99
n=1000000 worst (found) 20 average (found) 18.95
In one line: log₂ n is how many times n can be halved; binary search needs at most ⌊log₂ n⌋ + 1 comparisons, so a million names cost only 20.
Two sums that keep coming back
Almost every loop-counting question ends in one of two sums, so it is worth knowing both by heart and by picture.
The first is the story of the young Gauss, who was asked to add 1 + 2 + … + 100 and noticed that the first and last add to 101, the second and second-last add to 101 too, and there are 50 such pairs. In general, 1 + 2 + … + n = n(n + 1)/2. Here is a picture of the same trick for n = 6. The blue staircase has 1, 2, 3, 4, 5, 6 squares in its rows. The amber staircase is a second copy, turned over, and the two together fill a rectangle of 6 × 7 = 42 squares. So one staircase has 42/2 = 21 squares.
:::figure caption="Two staircases make a rectangle: 1 + 2 + … + 6 = 6 × 7 / 2 = 21" alt="A grid of six rows and seven columns. The blue squares form a staircase with 1, 2, up to 6 squares in the rows. The amber squares fill the rest, which is another staircase of 21 squares, so the whole grid has 42 squares." maxw=440
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 244" role="img"><rect x="41" y="21" width="28" height="28" rx="3" class="f-net"/><rect x="71" y="21" width="28" height="28" rx="3" class="f-host"/><rect x="101" y="21" width="28" height="28" rx="3" class="f-host"/><rect x="131" y="21" width="28" height="28" rx="3" class="f-host"/><rect x="161" y="21" width="28" height="28" rx="3" class="f-host"/><rect x="191" y="21" width="28" height="28" rx="3" class="f-host"/><rect x="221" y="21" width="28" height="28" rx="3" class="f-host"/><rect x="41" y="51" width="28" height="28" rx="3" class="f-net"/><rect x="71" y="51" width="28" height="28" rx="3" class="f-net"/><rect x="101" y="51" width="28" height="28" rx="3" class="f-host"/><rect x="131" y="51" width="28" height="28" rx="3" class="f-host"/><rect x="161" y="51" width="28" height="28" rx="3" class="f-host"/><rect x="191" y="51" width="28" height="28" rx="3" class="f-host"/><rect x="221" y="51" width="28" height="28" rx="3" class="f-host"/><rect x="41" y="81" width="28" height="28" rx="3" class="f-net"/><rect x="71" y="81" width="28" height="28" rx="3" class="f-net"/><rect x="101" y="81" width="28" height="28" rx="3" class="f-net"/><rect x="131" y="81" width="28" height="28" rx="3" class="f-host"/><rect x="161" y="81" width="28" height="28" rx="3" class="f-host"/><rect x="191" y="81" width="28" height="28" rx="3" class="f-host"/><rect x="221" y="81" width="28" height="28" rx="3" class="f-host"/><rect x="41" y="111" width="28" height="28" rx="3" class="f-net"/><rect x="71" y="111" width="28" height="28" rx="3" class="f-net"/><rect x="101" y="111" width="28" height="28" rx="3" class="f-net"/><rect x="131" y="111" width="28" height="28" rx="3" class="f-net"/><rect x="161" y="111" width="28" height="28" rx="3" class="f-host"/><rect x="191" y="111" width="28" height="28" rx="3" class="f-host"/><rect x="221" y="111" width="28" height="28" rx="3" class="f-host"/><rect x="41" y="141" width="28" height="28" rx="3" class="f-net"/><rect x="71" y="141" width="28" height="28" rx="3" class="f-net"/><rect x="101" y="141" width="28" height="28" rx="3" class="f-net"/><rect x="131" y="141" width="28" height="28" rx="3" class="f-net"/><rect x="161" y="141" width="28" height="28" rx="3" class="f-net"/><rect x="191" y="141" width="28" height="28" rx="3" class="f-host"/><rect x="221" y="141" width="28" height="28" rx="3" class="f-host"/><rect x="41" y="171" width="28" height="28" rx="3" class="f-net"/><rect x="71" y="171" width="28" height="28" rx="3" class="f-net"/><rect x="101" y="171" width="28" height="28" rx="3" class="f-net"/><rect x="131" y="171" width="28" height="28" rx="3" class="f-net"/><rect x="161" y="171" width="28" height="28" rx="3" class="f-net"/><rect x="191" y="171" width="28" height="28" rx="3" class="f-net"/><rect x="221" y="171" width="28" height="28" rx="3" class="f-host"/><text x="32" y="40" text-anchor="end" font-size="12" class="f-mute">1</text><text x="32" y="70" text-anchor="end" font-size="12" class="f-mute">2</text><text x="32" y="100" text-anchor="end" font-size="12" class="f-mute">3</text><text x="32" y="130" text-anchor="end" font-size="12" class="f-mute">4</text><text x="32" y="160" text-anchor="end" font-size="12" class="f-mute">5</text><text x="32" y="190" text-anchor="end" font-size="12" class="f-mute">6</text><text x="32" y="14" text-anchor="end" font-size="12" class="f-mute">i</text><text x="145" y="218" text-anchor="middle" font-size="12" class="f-mute">7 columns</text><text x="260" y="44" text-anchor="start" font-size="12" class="f-ink">blue staircase:</text><text x="260" y="62" text-anchor="start" font-size="12" font-weight="700" class="f-ink">1+2+...+6 = 21</text><text x="260" y="106" text-anchor="start" font-size="12" class="f-host">amber staircase:</text><text x="260" y="124" text-anchor="start" font-size="12" font-weight="700" class="f-host">21</text><text x="260" y="168" text-anchor="start" font-size="12" class="f-ink">together:</text><text x="260" y="186" text-anchor="start" font-size="12" font-weight="700" class="f-ink">6 x 7 = 42</text></svg>
:::
For n = 1000 the sum is 500,500, and the program that adds it up gets exactly that. Notice the size: n(n + 1)/2 is about n²/2. Whenever an inner loop runs 1, then 2, then 3 times and so on, you are looking at this staircase, and the work is of order n², not n.
The second sum is the doubling series: 1 + 2 + 4 + … + 2ᵏ = 2ᵏ⁺¹ − 1. Why? In binary, 1 + 2 + 4 + 8 is 1111, and adding 1 more makes the carries run all the way and gives 10000, which is 16. So the sum is one less than the next power of two. The useful thing to remember is that the whole sum is smaller than twice its last term: 1 + 2 + 4 + … + 512 = 1,023, just under 2 × 512. The last term alone is more than half of everything. Part 3 will use this to explain a loop that looks like n log n but isn't, and Part 6 uses it to explain why doubling an array is cheap.
:::check id=p1-check
A sorted list holds 1,000,000 names, and a program searches it by halving the range each time. At most how many comparisons does it need?
- 10
- 20
- 500,000
- 1,000,000
Each comparison halves the range, so the worst case is ⌊log₂ n⌋ + 1 = ⌊19.93⌋ + 1 = 20 (the real run above gave 20). A thousand times more data adds only about 10 to the count, which is why 10 is too small: 2¹⁰ is only 1,024, enough for a thousand names, not a million. 500,000 is about what linear search needs on average, and 1,000,000 is its worst case.
:::
Read the whole lesson free with an account · পুরো পাঠ পড়তে বিনামূল্যে সাইন আপ করুন
All 13 lessons · সব পাঠ
- Complexity Analysis and Big-O Notation · কমপ্লেক্সিটি অ্যানালাইসিস ও Big-O নোটেশন
- Arrays and Strings · অ্যারে ও স্ট্রিং
- Linked Lists · লিংকড লিস্ট
- Stacks and Expressions · স্ট্যাক ও এক্সপ্রেশন
- Queues, Circular Queues and Deques · কিউ, সার্কুলার কিউ ও ডেক
- Trees: Vocabulary, Traversals and the Binary Search Tree · ট্রি: পরিভাষা, ট্রাভার্সাল ও বাইনারি সার্চ ট্রি
- Balanced Trees and Heaps: AVL, Heap and Priority Queue · ব্যালান্সড ট্রি ও হিপ: AVL, Heap ও প্রায়োরিটি কিউ
- Graphs and Their Representations · গ্রাফ ও তাদের উপস্থাপনা
- Hashing and Hash Tables · হ্যাশিং ও হ্যাশ টেবিল
- B-trees, Tries, Union-Find and Choosing a Structure · B-tree, Trie, Union-Find ও ডেটা স্ট্রাকচার বেছে নেওয়া
- Queues and Trees in C: the Code the Exam Asks For · সি-তে কিউ ও ট্রি: পরীক্ষায় যে কোড চাওয়া হয়
- Trees II: Threaded, General-to-Binary, Red-Black, Splay and 2-3 Trees, k-th Largest · ট্রি II: থ্রেডেড ট্রি, জেনারেল থেকে বাইনারি, রেড-ব্ল্যাক, স্প্লে ও ২-৩ ট্রি, k-তম বৃহত্তম
- Revision sheet: Data Structures · রিভিশন শিট: ডেটা স্ট্রাকচার