Solve InterviewCake's merge sorted arrays question
Write a function merging two sorted arrays into one sorted array.
This commit is contained in:
		
							parent
							
								
									00e3526d9a
								
							
						
					
					
						commit
						737bdd0a23
					
				
					 2 changed files with 64 additions and 1 deletions
				
			
		
							
								
								
									
										63
									
								
								scratch/deepmind/part_two/merge-sorted-arrays.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								scratch/deepmind/part_two/merge-sorted-arrays.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,63 @@ | ||||||
|  | function mergeArrays(xs: Array<number>, ys: Array<number>): Array<number> { | ||||||
|  |   let i = 0; | ||||||
|  |   let j = 0; | ||||||
|  |   const result = []; | ||||||
|  | 
 | ||||||
|  |   for (let q = 0; q < xs.length + ys.length; q += 1) { | ||||||
|  |     if (i === xs.length) { | ||||||
|  |       while (j < ys.length) { | ||||||
|  |         result.push(ys[j]); | ||||||
|  |         j += 1; | ||||||
|  |       } | ||||||
|  |     } else if (j === ys.length) { | ||||||
|  |       while (i < xs.length) { | ||||||
|  |         result.push(xs[i]); | ||||||
|  |         i += 1; | ||||||
|  |       } | ||||||
|  |     } else if (xs[i] < ys[j]) { | ||||||
|  |       result.push(xs[i]); | ||||||
|  |       i += 1; | ||||||
|  |     } else { | ||||||
|  |       result.push(ys[j]); | ||||||
|  |       j += 1; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   return result; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // Tests
 | ||||||
|  | let desc = "both arrays are empty"; | ||||||
|  | let actual = mergeArrays([], []); | ||||||
|  | let expected = []; | ||||||
|  | assertDeepEqual(actual, expected, desc); | ||||||
|  | 
 | ||||||
|  | desc = "first array is empty"; | ||||||
|  | actual = mergeArrays([], [1, 2, 3]); | ||||||
|  | expected = [1, 2, 3]; | ||||||
|  | assertDeepEqual(actual, expected, desc); | ||||||
|  | 
 | ||||||
|  | desc = "second array is empty"; | ||||||
|  | actual = mergeArrays([5, 6, 7], []); | ||||||
|  | expected = [5, 6, 7]; | ||||||
|  | assertDeepEqual(actual, expected, desc); | ||||||
|  | 
 | ||||||
|  | desc = "both arrays have some numbers"; | ||||||
|  | actual = mergeArrays([2, 4, 6], [1, 3, 7]); | ||||||
|  | expected = [1, 2, 3, 4, 6, 7]; | ||||||
|  | assertDeepEqual(actual, expected, desc); | ||||||
|  | 
 | ||||||
|  | desc = "arrays are different lengths"; | ||||||
|  | actual = mergeArrays([2, 4, 6, 8], [1, 7]); | ||||||
|  | expected = [1, 2, 4, 6, 7, 8]; | ||||||
|  | assertDeepEqual(actual, expected, desc); | ||||||
|  | 
 | ||||||
|  | function assertDeepEqual(a: Array<number>, b: Array<number>, desc: string) { | ||||||
|  |   const aStr = JSON.stringify(a); | ||||||
|  |   const bStr = JSON.stringify(b); | ||||||
|  |   if (aStr !== bStr) { | ||||||
|  |     console.log(`${desc} ... FAIL: ${aStr} != ${bStr}`); | ||||||
|  |   } else { | ||||||
|  |     console.log(`${desc} ... PASS`); | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -2,7 +2,7 @@ | ||||||
| ** DONE Merging Meeting Times | ** DONE Merging Meeting Times | ||||||
| ** DONE Reverse String in Place | ** DONE Reverse String in Place | ||||||
| ** DONE Reverse Words | ** DONE Reverse Words | ||||||
| ** TODO Merge Sorted Arrays | ** DONE Merge Sorted Arrays | ||||||
| ** TODO Cafe Order Checker | ** TODO Cafe Order Checker | ||||||
| * Hashing and hash tables | * Hashing and hash tables | ||||||
| ** TODO Inflight Entertainment | ** TODO Inflight Entertainment | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue