PageSourceSearch

https://www.techinterviewhandbook.org/assets/js/44706460.80fb2fca.js

js techinterviewhandbook.org collected 2026-10-03 19:24:03 UTC 20,497 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunk_tih_website=globalThis.webpackChunk_tih_website||[]).push([[8234],{2403(e,t,n){n.d(t,{xA:()=>u,yg:()=>d});var a=n(1855);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,a)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach(function(t){r(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function s(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},i=Object.keys(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var l=a.createContext({}),g=function(e){var t=a.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},u=function(e){var t=g(e.components);return a.createElement(l.Provider,{value:t},e.children)},c={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},m=a.forwardRef(function(e,t){var n=e.components,r=e.mdxType,i=e.originalType,l=e.parentName,u=s(e,["components","mdxType","originalType","parentName"]),m=g(n),d=r,p=m["".concat(l,".").concat(d)]||m[d]||c[d]||i;return n?a.createElement(p,o(o({ref:t},u),{},{components:n})):a.createElement(p,o({ref:t},u))});function d(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var i=n.length,o=new Array(i);o[0]=m;var s={};for(var l in t)hasOwnProperty.call(t,l)&&(s[l]=t[l]);s.originalType=e,s.mdxType="string"==typeof e?e:r,o[1]=s;for(var g=2;g<i;g++)o[g]=n[g];return a.createElement.apply(null,o)}return a.createElement.apply(null,n)}m.displayName="MDXCreateElement"},3861(e,t,n){n.d(t,{Ay:()=>o});var a=n(9932),r=(n(1855),n(2403));const i={toc:[{value:"AlgoMonster",id:"algomonster",level:3},{value:"Grokking the Coding Interview: Patterns for Coding Questions",id:"grokking-the-coding-interview-patterns-for-coding-questions",level:3},{value:"Master the Coding Interview: Data Structures + Algorithms",id:"master-the-coding-interview-data-structures--algorithms",level:3}]};function o(e){let{components:t,...n}=e;return(0,r.yg)("wrapper",(0,a.A)({},i,n,{components:t,mdxType:"MDXLayout"}),(0,r.yg)("h3",{id:"algomonster"},(0,r.yg)("a",{parentName:"h3",href:"https://shareasale.com/r.cfm?b=1873647&u=3114753&m=114505&urllink=&afftrack="},"AlgoMonster")),(0,r.yg)("p",null,"AlgoMonster aims to help you ace the technical interview ",(0,r.yg)("strong",{parentName:"p"},"in the shortest time possible"),". By Google engineers, AlgoMonster uses a data-driven approach to teach you the most useful key question patterns and has contents to help you quickly revise basic data structures and algorithms. Best of all, AlgoMonster is not subscription-based - pay a one-time fee and get ",(0,r.yg)("strong",{parentName:"p"},"lifetime access"),". ",(0,r.yg)("a",{parentName:"p",href:"https://shareasale.com/r.cfm?b=1873647&u=3114753&m=114505&urllink=&afftrack="},(0,r.yg)("strong",{parentName:"a"},"Join today for a 70% discount \u2192"))),(0,r.yg)("h3",{id:"grokking-the-coding-interview-patterns-for-coding-questions"},(0,r.yg)("a",{parentName:"h3",href:"https://www.designgurus.io/course/grokking-the-coding-interview?aff=kJSIoU"},"Grokking the Coding Interview: Patterns for Coding Questions")),(0,r.yg)("p",null,"This course on by Design Gurus expands upon the questions on the recommended practice questions but approaches the practicing from a questions pattern perspective, which is an approach I also agree with for learning and have personally used to get better at coding interviews. The course allows you to practice selected questions in Java, Python, C++, JavaScript and also provides sample solutions in those languages along with step-by-step visualizations. ",(0,r.yg)("strong",{parentName:"p"},"Learn and understand patterns, not memorize answers!")," ",(0,r.yg)("a",{parentName:"p",href:"https://www.designgurus.io/course/grokking-the-coding-interview?aff=kJSIoU"},(0,r.yg)("strong",{parentName:"a"},"Get lifetime access now \u2192"))),(0,r.yg)("h3",{id:"master-the-coding-interview-data-structures--algorithms"},(0,r.yg)("a",{parentName:"h3",href:"https://www.udemy.com/course/master-the-coding-interview-data-structures-algorithms/"},"Master the Coding Interview: Data Structures + Algorithms")),(0,r.yg)("p",null,"This Udemy bestseller is one of the highest-rated interview preparation course (4.6 stars, 21.5k ratings, 135k students) and packs ",(0,r.yg)("strong",{parentName:"p"},"19 hours")," worth of contents into it. Like Tech Interview Handbook, it goes beyond coding interviews and covers resume, non-technical interviews, negotiations. It's an all-in-one package! Note that JavaScript is being used for the coding demos. ",(0,r.yg)("a",{parentName:"p",href:"https://www.udemy.com/course/master-the-coding-interview-data-structures-algorithms/"},(0,r.yg)("strong",{parentName:"a"},"Check it out \u2192"))))}o.isMDXComponent=!0},6004(e,t,n){n.r(t),n.d(t,{assets:()=>g,contentTitle:()=>s,default:()=>m,frontMatter:()=>o,metadata:()=>l,toc:()=>u});var a=n(9932),r=(n(1855),n(2403)),i=n(3861);const o={id:"sorting-searching",title:"Sorting and searching cheatsheet for coding interviews",description:"Sorting and searching study guide for coding interviews, including practice questions, te
1chniques, time complexity, and recommended resources",keywords:["sorting searching coding interview study guide","sorting searching tips for coding interviews","sorting searching practice questions","sorting searching useful techniques","sorting searching time complexity","sorting searching recommended study resources"],sidebar_label:"Sorting and searching",toc_max_heading_level:2},s=void 0,l={unversionedId:"algorithms/sorting-searching",id:"algorithms/sorting-searching",title:"Sorting and searching cheatsheet for coding interviews",description:"Sorting and searching study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources",source:"@site/contents/algorithms/sorting-searching.md",sourceDirName:"algorithms",slug:"/algorithms/sorting-searching",permalink:"/algorithms/sorting-searching",draft:!1,tags:[],version:"current",lastUpdatedAt:1786104387,formattedLastUpdatedAt:"Aug 7, 2026",frontMatter:{id:"sorting-searching",title:"Sorting and searching cheatsheet for coding interviews",description:"Sorting and searching study guide for coding interviews, including practice questions, techniques, time complexity, and recommended resources",keywords:["sorting searching coding interview study guide","sorting searching tips for coding interviews","sorting searching practice questions","sorting searching useful techniques","sorting searching time complexity","sorting searching recommended study resources"],sidebar_label:"Sorting and searching",toc_max_heading_level:2},sidebar:"docs",previous:{title:"Recursion",permalink:"/algorithms/recursion"},next:{title:"Matrix",permalink:"/algorithms/matrix"}},g={},u=[{value:"Introduction",id:"introduction",level:2},{value:"Learning resources",id:"learning-resources",level:2},{value:"Time complexity",id:"time-complexity",level:2},{value:"Things to look out for during interviews",id:"things-to-look-out-for-during-interviews",level:2},{value:"Corner cases",id:"corner-cases",level:2},{value:"Techniques",id:"techniques",level:2},{value:"Sorted inputs",id:"sorted-inputs",level:3},{value:"Sorting an input that has limited range",id:"sorting-an-input-that-has-limited-range",level:3},{value:"Essential questions",id:"essential-questions",level:2},{value:"Recommended practice questions",id:"recommended-practice-questions",level:2},{value:"Recommended courses",id:"recommended-courses",level:2}],c={toc:u};
1function m(e){let{components:t,...n}=e;return(0,r.yg)("wrapper",(0,a.A)({},c,n,{components:t,mdxType:"MDXLayout"}),(0,r.yg)("head",null,(0,r.yg)("meta",{property:"og:image",content:"https://www.techinterviewhandbook.org/social/algorithms/algorithms/algorithms-sorting-searching.png"})),(0,r.yg)("h2",{id:"introduction"},"Introduction"),(0,r.yg)("p",null,"Sorting is the act of rearranging elements in a sequence in order, either in numerical or lexicographical order, and either ascending or descending."),(0,r.yg)("p",null,"A number of basic algorithms run in O(n",(0,r.yg)("sup",null,"2"),") and should not be used in interviews. In algorithm interviews, you're unlikely to need to implement any of the sorting algorithms from scratch. Instead you would need to sort the input using your language's default sorting function so that you can use binary searches on them."),(0,r.yg)("p",null,"On a sorted array of elements, by leveraging on its sorted property, searching can be done on them in faster than O(n) time by using a binary search. Binary search compares the target value with the middle element of the array, which informs the algorithm whether the target value lies in the left half or the right half, and this comparison proceeds on the remaining half until the target is found or the remaining half is empty."),(0,r.yg)("h2",{id:"learning-resources"},"Learning resources"),(0,r.yg)("p",null,"While you're unlikely to be asked to implement a sorting algorithm from scratch during an interview, it is good to know the various time complexities of the different sorting algorithms."),(0,r.yg)("ul",null,(0,r.yg)("li",{parentName:"ul"},"Readings",(0,r.yg)("ul",{parentName:"li"},(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/sorting-out-the-basics-behind-sorting-algorithms-b0a032873add"},"Sorting Out The Basics Behind Sorting Algorithms"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.khanacademy.org/computing/computer-science/algorithms/binary-search/a/binary-search"},"Binary Search"),", Khan Academy"))),(0,r.yg)("li",{parentName:"ul"},"Additional (only if you have time)",(0,r.yg)("ul",{parentName:"li"},(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/exponentially-easy-selection-sort-d7a34292b049"},"Exponentially Easy Selection Sort"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/bubbling-up-with-bubble-sorts-3df5ac88e592"},"Bubbling Up With Bubble Sorts"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/inching-toward-insertion-sort-9799274430da"},"Inching Towards Insertion Sort"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/making-sense-of-merge-sort-part-1-49649a143478"},"Making Sense of Merge Sort (Part 1)"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/making-sense-of-merge-sort-part-2-be8706453209"},"Making Sense of Merge Sort (Part 2)"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/pivoting-to-understand-quicksort-part-1-75178dfb9313"},"Pivoting To Understand Quicksort (Part 1)"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/pivoting-to-understand-quicksort-part-2-30161aefe1d3"},"Pivoting To Understand Quicksort (Part 2)"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/counting-linearly-with-counting-sort-cd8516ae09b3"},"Counting Linearly With Counting Sort"),", basecs"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://medium.com/basecs/getting-to-the-root-of-sorting-with-radix-sort-f8e9240d4224"},"Getting To The Root Of Sorting With Radix Sort"),", basecs"))),(0,r.yg)("li",{parentName:"ul"},"Videos",(0,r.yg)("ul",{parentName:"li"},(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=ryRfapIQHW0&feature=youtu.be"},"Heapsort")," (",(0,r.yg)("a",{parentName:"li",href:"https://samuelalbanie.com/files/digest-slides/2022-12-brief-guide-to-heapsort-and-binary-heaps.pdf"},"slides"),"), Samuel Albanie, University of Cambridge"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=kbiKn1K08RM&feature=youtu.be"},"Quicksort")," (",(0,r.yg)("a",{parentName:"li",href:"https://samuelalbanie.com/files/digest-slides/2023-01-brief-guide-to-quicksort.pdf"},"slides"),"), Samuel Albanie, University of Cambridge"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=JWSiXs9aB5U"},"Lower bounds for comparison sorts")," (",(0,r.yg)("a",{parentName:"li",href:"https://samuelalbanie.com/files/digest-slides/2023-01-brief-guide-to-comparison-sorting-lower-bounds.pdf"},"slides"),"), Samuel Albanie, University of Cambridge"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=0aMcZpAySjw"},"Counting sort")," (",(0,r.yg)("a",{parentName:"li",href:"https://samuelalbanie.com/files/digest-slides/2023-01-brief-guide-to-counting-sort.pdf"},"slides"),"), Samuel Albanie, University of Cambridge"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=HzPbzQi9404"},"Radix sort")," (",(0,r.yg)("a",{parentName:"li",href:"https://samuelalbanie.com/files/digest-slides/2023-01-brief-guide-to-radix-sort.pdf"},"slides"),"), Samuel Albanie, University of Cambridge"),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://www.youtube.com/watch?v=mz2fBJyoEVc"},"Bucket sort")," (",(0,r.yg)("a",{parentName:"li",href:"https://samuelalbanie.com/files/digest-slides/2023-01-brief-guide-to-bucket-sort.pdf"},"slides"),"), Samuel Albanie, University of Cambridge")))),(0,r.yg)("h2",{id:"time-complexity"},"Time complexity"),(0,r.yg)("table",null,(0,r.yg)("thead",{parentName:"table"},(0,r.yg)("tr",{parentName:"thead"},(0,r.yg)("th",{parentName:"tr",align:null},"Algorithm"),(0,r.yg)("th",{parentName:"tr",align:null},"Time"),(0,r.yg)("th",{parentName:"tr",align:null},"Space"))),(0,r.yg)("tbody",{parentName:"table"},(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Bubble sort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(n",(0,r.yg)("sup",null,"2"),")"),(0,r.yg)("td",{parentName:"tr",align:null},"O(1)")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Insertion sort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(n",(0,r.yg)("sup",null,"2"),")"),(0,r.yg)("td",{parentName:"tr",align:null},"O(1)")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Selection sort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(n",(0,r.yg)("sup",null,"2"),")"),(0,r.yg)("td",{parentName:"tr",align:null},"O(1)")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Quicksort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(nlog(n))"),(0,r.yg)("td",{parentName:"tr",align:null},"O(log(n))")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Mergesort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(nlog(n))"),(0,r.yg)("td",{parentName:"tr",align:null},"O(n)")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Heapsort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(nlog(n))"),(0,r.yg)("td",{parentName:"tr",align:null},"O(1)")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Counting sort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(n + k)"),(0,r.yg)("td",{parentName:"tr",align:null},"O(k)")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Radix sort"),(0,r.yg)("td",{parentName:"tr",align:null},"O(nk)"),(0,r.yg)("td",{parentName:"tr",align:null},"O(n + k)")))),(0,r.yg)("table",null,(0,r.yg)("thead",{parentName:"table"},(0,r.yg)("tr",{parentName:"thead"},(0,r.yg)("th",{parentName:"tr",align:null},"Algorithm"),(0,r.yg)("th",{parentName:"tr",align:null},"Big-O"))),(0,r.yg)("tbody",{parentName:"table"},(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:null},"Binary search"),(0,r.yg)("td",{parentName:"tr",align:null},"O(log(n))")))),(0,r.yg)("h2",{id:"things-to-look-out-for-during-interviews"},"Things to look out for during interviews"),(0,r.yg)("p",null,"Make sure you know the time and space complexity of the language's default sorting algorithm! The time complexity is almost definitely O(n log n). Bonu
1s points if you can name the sort. In Python 3.11+, it's ",(0,r.yg)("a",{parentName:"p",href:"https://www.wild-inter.net/posts/powersort-in-python-3.11"},"Powersort"),", which replaced Timsort as the default sorting algorithm. In Java, ",(0,r.yg)("a",{parentName:"p",href:"https://github.com/openjdk/jdk/blob/d9052b946682d1c0f2629455d73fe4e6b95b29db/src/java.base/share/classes/java/util/TimSort.java"},"an implementation of Timsort")," is used for sorting objects, and ",(0,r.yg)("a",{parentName:"p",href:"https://github.com/openjdk/jdk/blob/d9052b946682d1c0f2629455d73fe4e6b95b29db/src/java.base/share/classes/java/util/DualPivotQuicksort.java"},"Dual-Pivot Quicksort")," is used for sorting primitives."),(0,r.yg)("h2",{id:"corner-cases"},"Corner cases"),(0,r.yg)("ul",null,(0,r.yg)("li",{parentName:"ul"},"Empty sequence"),(0,r.yg)("li",{parentName:"ul"},"Sequence with one element"),(0,r.yg)("li",{parentName:"ul"},"Sequence with two elements"),(0,r.yg)("li",{parentName:"ul"},"Sequence containing duplicate elements.")),(0,r.yg)("h2",{id:"techniques"},"Techniques"),(0,r.yg)("h3",{id:"sorted-inputs"},"Sorted inputs"),(0,r.yg)("p",null,"When a given sequence is in a sorted order (be it ascending or descending), using binary search should be one of the first things that come to your mind."),(0,r.yg)("h3",{id:"sorting-an-input-that-has-limited-range"},"Sorting an input that has limited range"),(0,r.yg)("p",null,(0,r.yg)("a",{parentName:"p",href:"https://en.wikipedia.org/wiki/Counting_sort"},"Counting sort")," is a non-comparison-based sort you can use on numbers where you know the range of values beforehand. Examples: ",(0,r.yg)("a",{parentName:"p",href:"https://leetcode.com/problems/h-index/"},"H-Index")),(0,r.yg)("h2",{id:"essential-questions"},"Essential questions"),(0,r.yg)("p",null,(0,r.yg)("em",{parentName:"p"},"These are essential questions to practice if you're studying for this topic.")),(0,r.yg)("ul",null,(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/binary-search/"},"Binary Search")),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/search-in-rotated-sorted-array/"},"Search in Rotated Sorted Array"))),(0,r.yg)("h2",{id:"recommended-practice-questions"},"Recommended practice questions"),(0,r.yg)("p",null,(0,r.yg)("em",{parentName:"p"},"These are recommended questions to practice after you have studied for the topic and have practiced the essential questions.")),(0,r.yg)("ul",null,(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/"},"Kth Smallest Element in a Sorted Matrix")),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/search-a-2d-matrix/"},"Search a 2D Matrix")),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/kth-largest-element-in-an-array/"},"Kth Largest Element in an Array")),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/"},"Find Minimum in Rotated Sorted Array")),(0,r.yg)("li",{parentName:"ul"},(0,r.yg)("a",{parentName:"li",href:"https://leetcode.com/problems/median-of-two-sorted-arrays/"},"Median of Two Sorted Arrays"))),(0,r.yg)("h2",{id:"recommended-courses"},"Recommended courses"),(0,r.yg)(i.Ay,{mdxType:"AlgorithmCourses"}))}m.isMDXComponent=!0}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.