0
0
Typescriptprogramming~20 mins

Generic interface for collections in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Collection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this generic collection interface usage?

Consider the following TypeScript code using a generic interface for a collection. What will be logged to the console?

Typescript
interface Collection<T> {
  add(item: T): void;
  get(index: number): T | undefined;
  size(): number;
}

class SimpleCollection<T> implements Collection<T> {
  private items: T[] = [];

  add(item: T): void {
    this.items.push(item);
  }

  get(index: number): T | undefined {
    return this.items[index];
  }

  size(): number {
    return this.items.length;
  }
}

const numbers: Collection<number> = new SimpleCollection<number>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
console.log(numbers.get(1));
console.log(numbers.size());
A20\n3
B20\n2
Cundefined\n3
D10\n3
Attempts:
2 left
💡 Hint

Remember that arrays are zero-indexed and get(1) returns the second item.

🧠 Conceptual
intermediate
1:30remaining
Which statement about generic interfaces is true?

Choose the correct statement about generic interfaces in TypeScript.

AGeneric interfaces automatically infer the type without specifying it anywhere.
BGeneric interfaces can only be used with primitive types like number or string.
CGeneric interfaces cannot have methods that use the generic type parameter.
DGeneric interfaces allow you to define a contract that works with any data type specified when used.
Attempts:
2 left
💡 Hint

Think about how generics help reuse code for different types.

🔧 Debug
advanced
2:00remaining
What error does this code produce?

Examine the following TypeScript code. What error will the compiler report?

Typescript
interface Collection<T> {
  add(item: T): void;
  get(index: number): T;
}

class MyCollection<T> implements Collection<T> {
  private items: T[] = [];

  add(item: T): void {
    this.items.push(item);
  }

  get(index: number): T {
    return this.items[index];
  }
}

const col = new MyCollection<number>();
console.log(col.get(0));
AError: Property 'get' may return undefined but signature requires 'T'.
BError: Cannot assign type 'number' to type 'T'.
CNo error, code runs and logs 'undefined'.
DError: Missing implementation of 'size' method.
Attempts:
2 left
💡 Hint

Think about what happens if the array is empty and you try to get an item.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a generic interface for a collection?

Choose the correct syntax for declaring a generic interface named Collection with a method add that accepts an item of generic type T.

Ainterface Collection { add<T>(item: T): void; }
Binterface Collection<T> { add(item: T): void; }
Cinterface Collection<T> { add<T>(item: T): void; }
Dinterface Collection { add(item: T): void; }
Attempts:
2 left
💡 Hint

Remember that the generic parameter should be declared on the interface, not the method, for this case.

🚀 Application
expert
2:30remaining
How many items are in the resulting collection after this code runs?

Given the following code using a generic collection interface and class, how many items does the collection contain at the end?

Typescript
interface Collection<T> {
  add(item: T): void;
  remove(item: T): boolean;
  size(): number;
}

class AdvancedCollection<T> implements Collection<T> {
  private items: T[] = [];

  add(item: T): void {
    if (!this.items.includes(item)) {
      this.items.push(item);
    }
  }

  remove(item: T): boolean {
    const index = this.items.indexOf(item);
    if (index >= 0) {
      this.items.splice(index, 1);
      return true;
    }
    return false;
  }

  size(): number {
    return this.items.length;
  }
}

const col = new AdvancedCollection<string>();
col.add("apple");
col.add("banana");
col.add("apple");
col.remove("banana");
col.add("cherry");
col.remove("date");
A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Consider how duplicates are handled and what happens when removing items not in the collection.