jsartisan / frontend-challenges

FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
https://frontend-challenges.com
43 stars 5 forks source link

Implement Prefix Tree (Trie) #263

Closed jsartisan closed 1 hour ago

jsartisan commented 3 hours ago

Info

difficulty: medium
title: Implement Prefix Tree (Trie)
type: question
template: typescript
tags: javascript, blind75, trie, data-structure

Question

Implement a prefix tree (trie) that supports string insertion and search operations.

The PrefixTree class should support:

Constraints:

Examples:

const trie = new PrefixTree();
trie.insert("dog");
trie.search("dog");     // returns true
trie.search("do");      // returns false
trie.startsWith("do"); // returns true
trie.insert("do");
trie.search("do");      // returns true

Template

index.ts

export class PrefixTree {
  constructor() {

  }

  insert(word: string): void {

  }

  search(word: string): boolean {

  }

  startsWith(prefix: string): boolean {

  }
}

index.test.ts

import { PrefixTree } from './index';

describe('PrefixTree', () => {
  test('Example 1: Basic operations', () => {
    const trie = new PrefixTree();
    trie.insert("dog");
    expect(trie.search("dog")).toBe(true);
    expect(trie.search("do")).toBe(false);
    expect(trie.startsWith("do")).toBe(true);
    trie.insert("do");
    expect(trie.search("do")).toBe(true);
  });

  test('Empty string', () => {
    const trie = new PrefixTree();
    trie.insert("");
    expect(trie.search("")).toBe(true);
    expect(trie.startsWith("")).toBe(true);
  });

  test('Nested prefixes', () => {
    const trie = new PrefixTree();
    trie.insert("app");
    trie.insert("apple");
    trie.insert("apples");
    expect(trie.search("app")).toBe(true);
    expect(trie.search("appl")).toBe(false);
    expect(trie.search("apple")).toBe(true);
    expect(trie.search("apples")).toBe(true);
    expect(trie.startsWith("app")).toBe(true);
    expect(trie.startsWith("appl")).toBe(true);
  });

  test('Case sensitivity', () => {
    const trie = new PrefixTree();
    trie.insert("hello");
    expect(trie.search("Hello")).toBe(false);
    expect(trie.startsWith("HELL")).toBe(false);
  });

  test('Non-existent words', () => {
    const trie = new PrefixTree();
    trie.insert("cat");
    expect(trie.search("cats")).toBe(false);
    expect(trie.search("ca")).toBe(false);
    expect(trie.startsWith("dog")).toBe(false);
  });

  test('Multiple words with same prefix', () => {
    const trie = new PrefixTree();
    trie.insert("car");
    trie.insert("cat");
    trie.insert("catch");
    expect(trie.startsWith("ca")).toBe(true);
    expect(trie.search("car")).toBe(true);
    expect(trie.search("cat")).toBe(true);
    expect(trie.search("catch")).toBe(true);
    expect(trie.search("cats")).toBe(false);
  });
});
github-actions[bot] commented 3 hours ago

264 - Pull Request updated.