1. Home
  2. Explore
  3. Computer Science
  4. JavaScript Data Structures

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

JavaScript Data Structures

  • 110Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Stacks
  • spaceFlip card
  • KI knew it
  • DI didn't know it
  • ←Previous card
  • →Next card

What ordering principle does a stack follow?

Tap to flipClick to flipspace

Last in, first out (LIFO): the most recently added item is removed first.

Or swipe the card

110 cards

  1. What ordering principle does a stack follow?

    Last in, first out (LIFO): the most recently added item is removed first.

  2. How does a browser back button use a stack?

    Each visited URL is pushed onto a stack; pressing Back pops the most recent URL and exposes the previous one.

  3. What are the traditional operations of a stack?

    Push adds an item, pop removes the top item, peek displays the top item without removing it, and size or length counts the items.

  4. How can a JavaScript array be used as a stack?

    Use push() to add items and pop() to remove the most recently added item; use length to count items.

  5. What is a palindrome?

    A word that is spelled the same forward and backward, such as bob or racecar.

  6. How can a stack be used to test whether a word is a palindrome?

    Push each character onto a stack, pop the characters to construct the reverse, and compare the reverse with the original word.

  7. What does the count variable track in a manually implemented stack?

    The number of items currently stored in the stack.

  8. What should pop() return when a stack is empty?

    undefined.

  9. How do peek() and pop() differ?

    peek() returns the top item without removing it; pop() returns and removes the top item.

  10. How is the top item indexed in the object-based stack implementation?

    It is stored at count - 1, because count points to the next available position.

  11. What distinguishes a set from an array?

    A set contains no duplicate items, and its values do not have a required order.

  12. What is the typical use of a set?

    Checking whether a particular item is present.

  13. How does the custom set has() method work?

    It returns true when indexOf(element) is not -1; otherwise, it returns false.

  14. What does the custom set add() method do when the element is already present?

    It does not add a duplicate and returns false.

  15. What does the custom set add() method return after adding a new element?

    true.

  16. How does the custom set remove() method work?

    It finds the element's index, removes one item with splice(), and returns true; if absent, it returns false.

  17. Which custom set methods are included in the ES6 Set?

    has, values, add, delete instead of remove, and size.

  18. How does ES6 Set.size differ from the custom set size method?

    ES6 size is a property, so it is accessed without parentheses; the custom implementation uses a method.

  19. How does ES6 Set.values() differ from the custom set's values()?

    ES6 Set.values() returns an iterator rather than an array.

  20. What does the ES6 Set.add() method return?

    The set itself, not a Boolean indicating whether insertion occurred.

  21. What is the union of two sets?

    A new set containing every element found in either set, with duplicates removed.

  22. What is the intersection of two sets?

    A new set containing only elements found in both sets.

  23. What is the difference between two sets?

    The elements in one set that are not present in the other set.

  24. What does it mean for one set to be a subset of another?

    Every element of the first set is contained in the second set.

  25. What ordering principle does a queue follow?

    First in, first out (FIFO): the earliest added item is removed first.

  26. How can an array implement a queue?

    Add items at the end with push() and remove items from the beginning with shift().

  27. What are the basic methods of a queue?

    enqueue adds an item, dequeue removes and returns the front item, front views the front item, size counts items, and isEmpty tests whether the queue has no items.

  28. How does front() differ from dequeue()?

    front() returns the first item without removing it; dequeue() removes and returns it.

  29. How is a priority queue different from a normal queue?

    Each item has a priority, and higher-priority items are placed ahead of lower-priority items.

  30. What happens when two items in a priority queue have the same priority?

    They are processed in the order in which they were enqueued.

  31. How is an item inserted into a priority queue?

    If the queue is empty, it is appended; otherwise, it is inserted before the first item with a lower priority, or appended if no such item exists.

  32. What information may a priority-queue dequeue() return?

    It may return the entire item-priority pair or only the item portion.

  33. What is a tree data structure?

    A hierarchical structure of nodes that branches from a top node into child nodes.

  34. What are the root, parent, child, and leaf nodes in a tree?

    The root is the top node; a parent has branches to children; a child descends from a parent; and a leaf has no children.

  35. What is a binary tree?

    A tree in which each node has at most two child branches.

  36. What ordering rule defines a binary search tree?

    Values in the left subtree are less than or equal to the parent, and values in the right subtree are greater than or equal to the parent.

  37. What is the average time complexity of lookup, insertion, and deletion in a binary search tree?

    O(log⁡n)O(\log n)O(logn), where nnn is the number of stored items.

  38. How does a binary search tree compare with an unsorted array and a hash table?

    It is generally faster than linear search in an unsorted array but slower than the corresponding average operations in a hash table.

  39. What does a node contain in the binary search tree implementation?

    A data value and references to a left node and a right node.

  40. How is a new value added to a binary search tree?

    Compare it with the current node, recursively move left if smaller or right if larger, and insert it at the first null child position.

  41. How are duplicate values handled when adding to the binary search tree?

    They are not added; the insertion returns null.

  42. How are the minimum and maximum values found in a binary search tree?

    The minimum is the leftmost node, and the maximum is the rightmost node.

  43. How does find() search a binary search tree?

    Starting at the root, it returns true if the value matches; otherwise, it recursively follows left for smaller values or right for larger values.

  44. How is a leaf node removed from a binary search tree?

    Its parent reference is set to null.

  45. How is a BST node with one child removed?

    The node is replaced by its only child.

  46. How is a BST node with two children removed?

    Replace its value with the leftmost value in its right subtree, then recursively remove that replacement node.

  47. What is tree height?

    The distance from the root node to a specified leaf node, measured in levels or edges as defined by the implementation.

  48. What are minimum and maximum tree height?

    Minimum height is the distance from the root to the first leaf encountered; maximum height is the distance from the root to the deepest leaf.

  49. When is a tree considered balanced in the described implementation?

    When its minimum and maximum heights differ by at most one.

  50. What is inorder traversal of a binary tree?

    Visit the left subtree, then the current node, then the right subtree.

  51. What is preorder traversal of a binary tree?

    Visit the current node, then the left subtree, then the right subtree.

  52. What is postorder traversal of a binary tree?

    Visit the left subtree, then the right subtree, then the current node.

  53. What is level-order traversal?

    A breadth-first traversal that visits every node at one tree level before moving to the next level.

  54. What is a linked list?

    A sequence of nodes in which each node stores an element and a reference to the next node.

  55. What are the head and size of a linked list?

    The head points to the first node, and size records the number of nodes.

  56. What does the last node in a singly linked list reference?

    null, because it has no following node.

  57. How do linked-list sizes compare with traditional array sizes?

    Linked lists have dynamic size, whereas traditional arrays have a fixed allocated size.

  58. What are linked lists' insertion and deletion advantages over arrays?

    Insertions and deletions are generally more efficient because links can be changed without shifting many elements.

  59. What random-access advantage do arrays have over linked lists?

    An array can access an item at a known index directly, while a linked list must be traversed from the head.

  60. Why can arrays waste memory compared with linked lists?

    Arrays may be allocated larger than currently needed to allow future growth; linked lists allocate nodes as needed.

  61. How does the linked-list add() operation work?

    Create a node, place it at the head if the list is empty, or traverse to the last node and link the new node there.

  62. How is the head node removed from a linked list?

    Set the head pointer to the current head's next node and decrement the length.

  63. How is an interior linked-list node removed?

    Set the previous node's next reference to the removed node's next reference.

  64. What does a linked-list index search return when an element is absent?

    -1.

  65. How does a linked list retrieve an element at a given index?

    Start at the head and follow next references until the traversal count equals the requested index.

  66. What is a trie?

    A specialized tree, also called a prefix tree, that stores associative data by breaking strings into letter-by-letter nodes.

  67. What does each trie node contain in the described implementation?

    A map of child letters and an end marker indicating whether the node completes a word.

  68. Why are tries useful for dictionary or word validation?

    They can determine whether a word exists by following one character at a time instead of checking every stored word.

  69. How does a trie distinguish a complete word from a prefix?

    A node has its end marker set to true when it represents the final letter of a stored word.

  70. How is a word added to a trie?

    Process its first letter, create a child node if needed, and recursively process the remaining substring until the end marker is set.

  71. What is a binary heap?

    A partially ordered binary tree in which each node has at most two children and the heap property relates parents to children.

  72. What is the heap property of a min-heap?

    Every parent is less than or equal to its child nodes.

  73. What is the heap property of a max-heap?

    Every parent is greater than or equal to its child nodes.

  74. Does the order of sibling nodes matter in a binary heap?

    No. The relative order of children on the same level is not important as long as the heap property holds.

  75. What makes a binary heap a complete binary tree?

    All levels are full except possibly the last, and the last level is filled from left to right.

  76. How is a binary heap commonly represented in an array?

    The root is stored at index 111, index 000 is left unused, and child-parent positions are computed mathematically.

  77. Using one-based heap indexing, how are a node's children found?

    For a node at index iii, the left child is at 2i2i2i and the right child is at 2i+12i+12i+1.

  78. Using one-based heap indexing, how is a node's parent found?

    The parent is at ⌊i/2⌋\lfloor i/2 \rfloor⌊i/2⌋.

  79. How is an item inserted into a min-heap?

    Append it at the end, compare it with its parent, and repeatedly swap it upward while it is smaller than its parent.

  80. How is the minimum item removed from a min-heap?

    Remove the root, move the last item to the root, and repeatedly swap it downward with the smaller child until the heap property is restored.

  81. How does heap sort use a min-heap?

    Insert every item into a min-heap, repeatedly remove the minimum, and append each removed value to a result array.

  82. What is the average and worst-case time complexity of heap sort?

    O(nlog⁡n)O(n\log n)O(nlogn) for both average and worst-case performance.

  83. What is a hash table?

    A structure that stores key-value pairs by converting a key into an array index using a hash function.

  84. What is the average time complexity of hash-table search, insertion, and deletion?

    O(1)O(1)O(1) on average.

  85. What properties should a hash function have?

    It should consistently map the same key to the same number and ideally distribute different keys to different indices.

  86. What is a hash collision?

    A collision occurs when two different keys produce the same hash index.

  87. How does separate chaining handle hash collisions?

    Store multiple key-value pairs in the same bucket and search that bucket for the requested key.

  88. How does the example hash function produce an array index?

    It sums the character codes of the key and returns the sum modulo the number of buckets.

  89. What happens when inserting a key that already exists in a hash table?

    The existing key's value is updated rather than adding a duplicate key-value pair.

  90. How is a key-value pair removed from a chained hash table?

    Hash the key to locate its bucket, then delete the matching pair; if it is the only pair, the bucket itself can be deleted.

  91. How is a value looked up in a hash table?

    Hash the key, access the resulting bucket, and search its entries for the matching key.

  92. What JavaScript structures commonly use hash-table behavior?

    JavaScript objects are commonly implemented or used as associative key-value mappings.

  93. What are the nodes and edges of a graph?

    Nodes, or vertices, are the data items; edges are the connections or relationships between them.

  94. What is the difference between directed and undirected graphs?

    Undirected edges have no direction; directed edges point from one node to another and may not point back.

  95. What is an adjacency list?

    A representation that associates each vertex with a collection of its neighboring vertices.

  96. What is an adjacency matrix?

    A two-dimensional array in which rows and columns represent nodes; a value indicates whether an edge exists between them.

  97. What size must an adjacency matrix have for a graph with nnn nodes?

    It must be an n×nn \times nn×n matrix.

  98. How does an adjacency matrix represent a directed edge?

    A one is placed where the row node has an edge pointing to the column node.

  99. What is an incidence matrix?

    A two-dimensional array whose rows represent nodes and whose columns represent edges.

  100. How does an incidence matrix represent an undirected edge?

    Place a one in the two node rows associated with that edge's column.

  101. How does an incidence matrix represent a directed edge?

    Use one for an edge leaving a node and negative one for an edge entering a node.

  102. How can graph edges be weighted?

    Store numerical values representing the cost, distance, strength, or another application-specific quantity of each edge.

  103. What are graph traversal algorithms?

    Algorithms that visit nodes in a graph to explore relationships or determine distances.

  104. How does breadth-first search explore a graph?

    It visits the start node's neighbors first, then their neighbors, proceeding outward one edge-distance level at a time.

  105. What does breadth-first search compute in the described implementation?

    An object mapping each node to its distance from the chosen root node.

  106. How are distances initialized in the described breadth-first search?

    All node distances start at infinity, except the root distance, which starts at zero.

  107. What data structure does breadth-first search use to track nodes awaiting exploration?

    A queue.

  108. When is a neighbor's distance set during breadth-first search?

    When its current distance is still infinity, it is set to the current node's distance plus one.

  109. Why are unreachable graph nodes left at infinity in breadth-first search?

    They cannot be reached from the chosen root node.

  110. When does the breadth-first search loop terminate?

    When the queue becomes empty.

Card 1 of 110
Card 1 of 110

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent