0
0
DSA Javascriptprogramming~10 mins

Tree vs Array vs Linked List When Hierarchy Matters in DSA Javascript - Interactive Comparison Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple array representing a list of fruits.

DSA Javascript
const fruits = ['apple', [1], 'banana'];
console.log(fruits);
Drag options to blanks, or click blank then click option'
A'potato'
B'carrot'
C'orange'
D'lettuce'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a vegetable instead of a fruit.
2fill in blank
medium

Complete the code to link nodes in a singly linked list.

DSA Javascript
function Node(value) {
  this.value = value;
  this.next = [1];
}

const first = new Node(1);
const second = new Node(2);
first.next = second;
console.log(first.next.value);
Drag options to blanks, or click blank then click option'
Anull
B0
Csecond
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined or a variable that is not yet defined.
3fill in blank
hard

Fix the error in the tree node constructor to correctly assign children as an empty array.

DSA Javascript
function TreeNode(value) {
  this.value = value;
  this.children = [1];
}

const root = new TreeNode('root');
console.log(root.children.length);
Drag options to blanks, or click blank then click option'
Aundefined
B{}
Cnull
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using object literal {} instead of array [] for children.
4fill in blank
hard

Fill both blanks to create a linked list node and link it to the next node.

DSA Javascript
function ListNode(value) {
  this.value = value;
  this.next = [1];
}

const node1 = new ListNode(10);
const node2 = new ListNode(20);
node1.next = [2];
console.log(node1.next.value);
Drag options to blanks, or click blank then click option'
Anull
Bnode2
Cundefined
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to undefined or 0 instead of null.
Not linking node1 to node2 correctly.
5fill in blank
hard

Fill all three blanks to create a tree node, add a child, and print the child's value.

DSA Javascript
function TreeNode(value) {
  this.value = value;
  this.children = [1];
}

const parent = new TreeNode('parent');
const child = new TreeNode('child');
parent.children.[2](child);
console.log(parent.children[[3]].value);
Drag options to blanks, or click blank then click option'
A[]
Bpush
C0
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push.
Using wrong index to access child.