Created
January 13, 2026 21:15
-
-
Save hotdang-ca/424452cd091ef6d563ac8ff01abbc01c to your computer and use it in GitHub Desktop.
Interview Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function get( | |
| dbList: number[], | |
| pivotId: number, | |
| pageSize: number, | |
| pageNum: number, | |
| ): number[] { | |
| // pivotId is where we want loop around; get it's index. | |
| const sliceIndex = dbList.indexOf(pivotId); | |
| // reconstruct the set to start at the sliceIndex, and | |
| // end just before, looping to the beginnibng when we | |
| // reach the end | |
| const beginPart = dbList.slice(0, sliceIndex); | |
| const endPart = dbList.slice(sliceIndex); | |
| const finalList = endPart.concat(beginPart); | |
| // now that we have a reasonable list, let's determine | |
| // the maximum possible page, and exit early if we can | |
| const maxPages = Math.floor(finalList.length / pageSize); | |
| if (pageNum > maxPages) { | |
| return []; | |
| } | |
| // prepare to reformat the list into pages | |
| // this is each page set, never as long as `pageSize` | |
| let pageSet = []; | |
| // this is the list of pageSets | |
| let pages = []; | |
| // keeps track of which page number we're on, | |
| // so that we can exist when we've run out of pages | |
| var numberOfElementsCountedPerSet = 0; | |
| // ready to re-format! iterate over the re-organized list | |
| // until we've hit the constraints | |
| for (var x = 0; x < finalList.length; x++) { | |
| // load up the number into this page set | |
| pageSet.push(finalList[x]); | |
| // increment how many we've let in | |
| numberOfElementsCountedPerSet++; | |
| // if we've reached pageSize | |
| // OR we have less elements than can fit an entire page... | |
| if (numberOfElementsCountedPerSet >= pageSize || x === finalList.length - 1) { | |
| // add this pageSet to the pages | |
| pages.push(pageSet); | |
| // empty out the pageSet for the next iteration | |
| pageSet = []; | |
| // reset the count of elements in a set for the next iteration | |
| numberOfElementsCountedPerSet = 0; | |
| } | |
| } | |
| // we now have a list of pageSets -- return the pageSet at the pageNum requested | |
| return pages[pageNum]; | |
| } | |
| // a full set of unique integers that meet the question constraints | |
| const fullSet = [73, 18, 52, 19, 42, 77, 60, 39, 20, 12, 0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 17, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 56, 57, 58, 59, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 162, 163, 164, 165, 166, 167, 16, 109, 3]; | |
| console.info('Start of list (from pivotId):'); | |
| for (let i = 0; i <= 2; i++) { | |
| console.log(get( | |
| fullSet, | |
| 42, | |
| 2, | |
| i, | |
| )); | |
| } | |
| console.info('\nNear end of list, and looping to just before pivotIndex'); | |
| for (let i = 80; i <= 1000; i++) { | |
| let setResult = get( | |
| fullSet, | |
| 42, | |
| 2, | |
| i, | |
| ); | |
| // suppress printing of empty sets; this output would be an empty array | |
| if (setResult.length === 0) { | |
| continue; | |
| } | |
| console.log(setResult); | |
| } | |
| /* Sample output */ | |
| /* | |
| Start of list (from pivotId): | |
| [ 42, 77 ] | |
| [ 60, 39 ] | |
| [ 20, 12 ] | |
| Near end of list, and looping to just before pivotIndex | |
| [ 16, 109 ] | |
| [ 3, 73 ] | |
| [ 18, 52 ] | |
| [ 19 ] | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment