bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
āœļøFill the BlankjavascriptMediumArrays & Hashing

Starter: keep an empty string

Fill the test case that proves empty text survives. Goal: Include an empty string as a real list item, not as missing data.

Starter step 13 of 20Work with strings

Notice why empty strings and delimiters matter.

Choose the case that would break a decoder that guesses boundaries.

Problem Brief

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Machine 1 (sender) has the function: string encode(vector strs) { // ... your code return encoded\string; } Machine 2 (receiver) has the function: vector decode(string s) { //... your code return strs; } So Machine 1 does: string encoded\string = encode(strs); and Machine 2 does: vector strs2 = decode(encoded\string); strs2 in Machine 2 should be the same as strs in Machine 1. Implement the encode and decode methods. You are not allowed to solve the problem using any serialize methods (such as eval).

Puzzle Hints
  1. Read the blank inside this exact statement: "const words = ["hi", ___];".

  2. An empty string is still a real item and must not disappear. The correct token works because it preserves the goal: Include an empty string as a real list item, not as missing data.

  3. Check the variable that is read immediately after the blank; that usually tells you what the blank must produce.

Asked at 7 companies
BloombergFacebookGoogle
Map & Set in JavaScriptref

JavaScript provides Map for key-value pairs and Set for unique values. Both offer O(1) average lookup, insert, and delete. Unlike plain objects, Map preserves insertion order and allows any type as keys.

-new Map() — O(1) get/set/has/delete
-new Set() — O(1) add/has/delete, auto-deduplicates
-Map.get(key) returns undefined if missing
-for...of iterates Map entries as [key, value]
const map = new Map();
map.set('a', 1);    // set key-value
map.get('a');        // 1
map.has('a');        // true

const set = new Set([1, 2, 2, 3]);
set.size;            // 3 (deduped)
set.has(2);          // true
Official docs →
Map & Set in JavaScriptref

JavaScript provides Map for key-value pairs and Set for unique values. Both offer O(1) average lookup, insert, and delete. Unlike plain objects, Map preserves insertion order and allows any type as keys.

-new Map() — O(1) get/set/has/delete
-new Set() — O(1) add/has/delete, auto-deduplicates
-Map.get(key) returns undefined if missing
-for...of iterates Map entries as [key, value]
const map = new Map();
map.set('a', 1);    // set key-value
map.get('a');        // 1
map.has('a');        // true

const set = new Set([1, 2, 2, 3]);
set.size;            // 3 (deduped)
set.has(2);          // true
Official docs →
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by
How to think: Hash Map / Setguide

You need O(1) lookups — checking if something exists, counting frequencies, or finding pairs.

1.Ask: "Am I searching for something repeatedly?" → Hash Map
2.Ask: "Do I need to check existence?" → Set
3.Ask: "Do I need to count occurrences?" → Map with value = count
4.Ask: "Do I need to find a pair that satisfies a condition?" → Store complement in Map
5.The tradeoff: O(n) extra space buys you O(1) per lookup

vs Nested loops (O(n²)): You're comparing every element against every other — a Map does it in one pass

vs Sorting (O(n log n)): You just need existence/frequency checks, not order

find pairtwo numbers thatfrequencycountduplicateanagramgroup by

Drag tokens into the blanks

""
null
1// Goal: Include an empty string as a real list item, not as missing data.
2function starterExample() {
3 const words = ["hi", ___];
4 return words.length;
5}