Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45a835a

Browse files
'Recurring Symbol' challenge complete (Closes #6)
Created a demo of using freeform objects in TypeScript. Completed Google interview question.
1 parent 8bed54e commit 45a835a

File tree

7 files changed

+164
-25
lines changed

7 files changed

+164
-25
lines changed

‎Data-Structures/Hash-Tables/Implementation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class HashTable {
99
this.data = new Array(numBuckets);
1010
}
1111

12-
public set(key: string, value: any): void {
12+
public insert(key: string, value: any): void {
1313
let address = this._hash(key);
1414

1515
if (!this.data[address]) {
@@ -94,9 +94,9 @@ if (import.meta.main) {
9494
const hashTable = new HashTable(16);
9595
// hashTable.testHashFunction();
9696

97-
hashTable.set('grapes', 27); // Θ(1)
98-
hashTable.set('apples', 6);
99-
hashTable.set('tangerines', 12);
97+
hashTable.insert('grapes', 27); // Θ(1)
98+
hashTable.insert('apples', 6);
99+
hashTable.insert('tangerines', 12);
100100

101101
console.log('apples:', hashTable.get('apples'));
102102
console.log('grapes:', hashTable.get('grapes'));

‎Playground/Challenges/Arrays/Compare_Arrays.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals, assertNotEquals } from "../../test_deps.ts";
1+
import { assertEquals, assertNotEquals } from "../../../test_deps.ts";
22

33
import { containsCommonItem } from "./Compare_Arrays.ts";
44

‎Playground/Challenges/Arrays/Compare_Arrays.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ export function containsCommonItemSyntacticSugar(arr1: Array<string>, arr2: Arra
5151
return arr1.some(item => arr2.includes(item));
5252
}
5353

54-
const array1 = ['a', 'b', 'c', 'x'];
55-
const array2 = ['z', 'y', 'x'];
56-
const array3 = ['z', 'y', 'i'];
5754

58-
console.log(containsCommonItemBF(array1, array2)); // O(a*b)
59-
console.log(containsCommonItemBF(array1, array3)); // O(a*b)
6055

61-
console.log(containsCommonItem(array1, array2)); // O(a+b)
62-
console.log(containsCommonItem(array1, array3)); // O(a+b)
56+
if (import.meta.main) {
57+
const array1 = ['a', 'b', 'c', 'x'];
58+
const array2 = ['z', 'y', 'x'];
59+
const array3 = ['z', 'y', 'i'];
60+
61+
console.log(containsCommonItemBF(array1, array2)); // O(a*b)
62+
console.log(containsCommonItemBF(array1, array3)); // O(a*b)
63+
64+
console.log(containsCommonItem(array1, array2)); // O(a+b)
65+
console.log(containsCommonItem(array1, array3)); // O(a+b)
66+
}

‎Playground/Challenges/Hash-Tables/First_Recurring_Character.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { assertEquals } from "../../../test_deps.ts";
2+
3+
import {
4+
firstRecurringNumber,
5+
findFirstRecurring,
6+
} from "./Recurring_Symbol.ts";
7+
8+
const array1 = [2, 5, 1, 2, 3, 5, 1, 2, 4];
9+
const array2 = [2, 1, 1, 2, 3, 5, 1, 2, 4];
10+
const array3 = [2, 3, 4, 5];
11+
const array4 = [2, 5, 5, 2, 3, 5, 1, 2, 4];
12+
const array5 = ["a", "z", "n", "z", "x", "g"];
13+
14+
//---------------------------------------------------------------------
15+
// ---------- UNIT TESTS ----------
16+
//---------------------------------------------------------------------
17+
18+
Deno.test({
19+
name: "Recurring Character",
20+
fn() {
21+
// ["a", "z", "n", "z", "x", "g"];
22+
assertEquals(findFirstRecurring(array5), "z");
23+
},
24+
});
25+
26+
Deno.test({
27+
name: "Recurring Number 1",
28+
fn() {
29+
// [2, 5, 1, 2, 3, 5, 1, 2, 4]
30+
assertEquals(firstRecurringNumber(array1), 2);
31+
},
32+
});
33+
34+
Deno.test({
35+
name: "Recurring Number 2",
36+
fn() {
37+
// [2, 1, 1, 2, 3, 5, 1, 2, 4]
38+
assertEquals(firstRecurringNumber(array2), 1);
39+
},
40+
});
41+
42+
Deno.test({
43+
name: "Recurring Number 3",
44+
fn() {
45+
// [2, 3, 4, 5]
46+
assertEquals(firstRecurringNumber(array3), undefined);
47+
},
48+
});
49+
50+
Deno.test({
51+
name: "Recurring Number 4",
52+
fn() {
53+
// [2, 5, 5, 2, 3, 5, 1, 2, 4]
54+
assertEquals(firstRecurringNumber(array4), 5);
55+
},
56+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ---------- Google Interview Question ----------
2+
// Given an array = [2,5,1,2,3,5,1,2,4]:
3+
// It should return 2
4+
5+
// Given an array = [2,1,1,2,3,5,1,2,4]:
6+
// It should return 1
7+
8+
// Given an array = [2,3,4,5]:
9+
// It should return undefined
10+
11+
// Bonus... What if we had this:
12+
// [2,5,5,2,3,5,1,2,4]
13+
// Return 5 because the pairs are before 2,2
14+
15+
16+
// Solution by CoffeelessProgrammer ---------------------------------------
17+
18+
/**
19+
* Finds the first recurring element of a numerical array.
20+
* @param {Array<number>} numArray
21+
* @returns {number | undefined} First repeated number, or undefined if none
22+
*/
23+
export function firstRecurringNumber(numArray: number[]): number | undefined {
24+
let seen = new Set<number>();
25+
26+
for (let i=0; i < numArray.length; ++i) {
27+
if (seen.has(numArray[i])) {
28+
return numArray[i];
29+
}
30+
seen.add(numArray[i]);
31+
}
32+
33+
return undefined;
34+
}
35+
36+
/**
37+
* Finds the first recurring element of a numerical or string array.
38+
* @param {Array<number> | Array<string>} arr
39+
* @returns {number | string | undefined} First repeated symbol, or undefined if none
40+
*/
41+
export function findFirstRecurring(arr: number[] | string[]): number | string | undefined {
42+
let seen = new Set();
43+
44+
for (let i=0; i < arr.length; ++i) {
45+
if (seen.has(arr[i])) {
46+
return arr[i];
47+
}
48+
seen.add(arr[i]);
49+
}
50+
51+
return undefined;
52+
}

‎Playground/Object_Demo.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
console.log('------------------------- Episode 1 -------------------------');
3+
const episode1 = {
4+
name: 'The Boy in the Iceberg',
5+
location: ['Shipwreck', 'Southern Water Tribe', "Zuko's Ship"],
6+
characters: ['Aang', 'Appa', 'Katara', 'Sokka', 'Iroh', 'Zuko'],
7+
introduction: function() {
8+
return "Water. Earth. Fire. Air. Long ago, the four nations lived together in harmony.";
9+
}
10+
};
11+
12+
console.log(episode1);
13+
14+
15+
16+
console.log('------------------------- Episode 2 -------------------------');
17+
const episode2 = Object.create(episode1);
18+
19+
console.log(episode2); // While fields don't show on console
20+
console.log(episode2.name, '—', episode2['characters']); // They are still populated by passed in prototype
21+
console.log(episode2.introduction()); // Function also exists because episode1 used as prototype
22+
23+
episode2.name = 'The Avatar Returns';
24+
episode2['location'] = ['Southern Water Tribe', "Zuko's Ship"]; // Recommended assignment syntax
25+
episode2['characters'] = ['Aang', 'Appa', 'Katara', 'Sokka', 'Iroh', 'Zuko'];
26+
27+
console.log(episode2);
28+
29+
30+
31+
console.log('------------------------- Episode 3 -------------------------');
32+
const episode3 = Object.create({});
33+
34+
// console.log(episode3.introduction()); // Does not exist because didn't use episode1 prototype
35+
36+
episode3.name = 'The Southern Air Temple';
37+
episode3['location'] = ['Earth Kingdom Harbor', 'Southern Air Temple']; // Recommended syntax
38+
episode3[1] = 'The Air Temple is one of the most beautiful places in the world!'; // Can also use syntax for numerical index in object
39+
40+
console.log(episode3);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /