Quick Revision

GK One-Line Question & Answer

15541+ short questions with short answers, covering every category and sub-category on the site — no long articles to scroll through. Good for a fast recap before an exam, or a few minutes of daily practice.

Data Structures and Algorithms → Introduction to DSA 32

The output of: auto f=function<int(int)>([](int n){return n*n;}); cout<<f(7);
49
click to copy
The output of: map<string,int> freq; string words[]={"a","b","a","c","b","a"}; for(auto w:words)freq[w]++; cout<<freq["a"]<<freq["b"]<<freq["c"];
321
click to copy
The output of: vector<pair<int,int>> v={{3,1},{1,3},{2,2}}; sort(v.begin(),v.end()); cout<<v[0].first<<v[0].second;
13
click to copy
The output of: priority_queue<pair<int,int>> pq; pq.push({1,2}); pq.push({3,1}); pq.push({2,3}); cout<<pq.top().first;
3
click to copy
The output of: auto it=find(begin({5,3,8,1,9}),end({5,3,8,1,9}),8); cout<<*it;
8
click to copy
What is the time complexity of std::sort?
O(n log n) average
click to copy
What is the space complexity of quicksort (average)?
O(log n) recursion stack
click to copy
Which sort is best for nearly sorted arrays?
Insertion Sort
click to copy
Counting sort is not a comparison sort and runs in:
O(n + k) where k is range of values
click to copy
Bucket sort is efficient when input is:
Uniformly distributed in a known range
click to copy
Tim sort (used in Python and Java) combines:
Insertion sort for small runs + merge sort
click to copy
The output of: int arr[]={5,2,8,1,9}; int n=5; for(int i=0;i<n-1;i++) for(int j=0;j<n-i-1;j++) if(arr[j]>arr[j+1]) swap(arr[j],arr[j+1]); cout<<arr[0];
1
click to copy
What is the maximum comparisons in binary search for n=1024 elements?
11
click to copy
Interpolation search works best when:
Data is uniformly distributed (better than binary search)
click to copy
Exponential search is useful when:
Array is sorted and size is unknown (infinite/very large)
click to copy
Jump search works by:
Jumping ahead by sqrt(n) steps then linear search back
click to copy
The output of: int a[]={1,1,2,2,3}; cout<<distance(a,unique(a,a+5));
3
click to copy
What is Big Theta of the best sort algorithm for general data?
O(n log n)
click to copy
The output of: int n; cin>>n; cout<<(n%15==0?"FizzBuzz":n%3==0?"Fizz":n%5==0?"Buzz":to_string(n)); with n=15:
FizzBuzz
click to copy
FizzBuzz for n=9 outputs:
Fizz
click to copy
FizzBuzz for n=10 outputs:
Buzz
click to copy
FizzBuzz for n=7 outputs:
7
click to copy
The output of: int n=4; int count=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j) count++; cout<<count;
12
click to copy
The output of: string s="aabbcc"; map<char,int> m; for(char c:s) m[c]++; for(auto [k,v]:m) if(v==1) cout<<k; if there were unique chars
No output (all appear twice)
click to copy
The output of: int arr[]={1,2,3,4,5}; cout<<*prev(end(arr));
5
click to copy
The output of: vector<int> v={1,2,3}; v.insert(v.end(),{4,5,6}); cout<<v.size();
6
click to copy
The output of: array<int,5> a={1,2,3,4,5}; cout<<a.back();
5
click to copy
The output of: array<int,5> a; a.fill(7); cout<<a[2];
7
click to copy
The output of: bitset<8> bs(170); cout<<bs;
10101010
click to copy
The output of: bitset<8> bs("10110100"); cout<<bs.count();
4
click to copy
bitset::flip() does:
Flips all bits (0 to 1 and 1 to 0)
click to copy
The output of: bitset<4> a("1010"), b("1100"); cout<<(a&b);
1000
click to copy

Data Structures and Algorithms → Sorting Algorithms 5

What is the space complexity of merge sort?
O(n)
click to copy
What is the space complexity of heap sort?
O(1) in-place
click to copy
Radix sort time complexity for n numbers with d digits:
O(d*n)
click to copy
Shell sort is a generalization of:
Insertion sort (with gap sequences)
click to copy
The output of: int arr[]={64,34,25,12,22,11,90}; // selection sort: for(int i=0;i<6;i++){int m=i; for(int j=i+1;j<7;j++) if(arr[j]<arr[m]) m=j; swap(arr[i],arr[m]);} cout<<arr[0];
11
click to copy

Data Structures and Algorithms → Linked List 1

Which sort is best for linked lists?
Merge Sort
click to copy

Data Structures and Algorithms → Arrays 2

Binary search on sorted array {1,3,5,7,9,11,13}: how many comparisons to find 7?
3
click to copy
Ternary search divides array into:
Three parts (finds max/min of unimodal function)
click to copy