1. Home
  2. Explore
  3. Computer Science
  4. Java Data Structures and Algorithms

Study methods

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

Java Data Structures and Algorithms

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

What is a data structure?

Tap to flipClick to flipspace

A data structure is a way of organizing information stored in a computer.

Or swipe the card

96 cards

  1. What is a data structure?

    A data structure is a way of organizing information stored in a computer.

  2. How can the choice of data structure affect a program?

    A data structure affects how efficiently an algorithm performs operations, uses memory, and utilizes the CPU.

  3. Why are different data structures used for different applications?

    Different data structures suit different applications because they organize and access data in different ways.

  4. What defines a linear data structure?

    A linear data structure arranges elements sequentially, one after another.

  5. What defines a non-linear data structure?

    A non-linear data structure organizes data in a non-sequential or hierarchical form; a node may have multiple neighbors.

  6. Classify arrays, stacks, queues, linked lists, trees, and graphs as linear or non-linear.

    Arrays, stacks, queues, and linked lists are linear structures; trees and graphs are non-linear structures.

  7. What is an algorithm?

    An algorithm is a step-by-step, language-independent approach to solving a problem.

  8. What is the purpose of algorithm analysis?

    Algorithm analysis compares resource use—especially time and space—to help identify an efficient approach.

  9. How do arrays and linked lists differ in their memory requirements?

    An array stores elements in contiguous memory locations, whereas linked-list nodes may occupy separate memory locations connected by references.

  10. How does an array provide access to a particular element?

    Array elements are accessed by index, allowing direct access to a specified position.

  11. What indices are valid for a zero-based array of length nnn?

    In a zero-based array, the first element is at index 000; for an array of length nnn, valid indices run from 000 to n−1n-1n−1.

  12. Why can an array support direct access using an index?

    For fixed-size elements, an array element’s address can be calculated from the base address, its index, and the element size. This supports direct indexed access.

  13. What kind of elements does a conventional array store?

    Arrays generally store homogeneous elements: values of the array’s declared type.

  14. How do the indices of one-dimensional and two-dimensional arrays differ?

    A one-dimensional array uses one index; a two-dimensional array uses two indices to identify a row and column.

  15. How can a two-dimensional array be visualized?

    A two-dimensional array can be viewed as a table or matrix with rows and columns.

  16. How can you declare and create a six-element integer array in Java?

    In Java, an array is declared with a type and name, then created with new and a size, such as int[] a = new int[6];.

  17. How can you initialize a Java array at the point of declaration?

    A Java array can be initialized with values directly, such as int[] a = {1, 2, 3};; the size is inferred from the values.

  18. How can a loop safely traverse every element of a Java array?

    A loop can visit each array index from 000 up to, but not including, the array’s length.

  19. Name key advantages of using an array.

    An array provides convenient indexed access and iteration, and can replace many separately named variables when storing collections of similar values.

  20. What limitations arise from an array’s fixed size?

    An array has a fixed capacity after creation; unused capacity can waste memory, and exceeding the capacity requires a different or resized structure.

  21. Why can insertion and deletion be costly in an array?

    Inserting or deleting within an array may require shifting elements; insertion can also fail if there is no spare capacity.

  22. What memory-allocation constraint does an array have?

    An array requires a sufficiently large contiguous block of memory.

  23. What ordering principle does a stack follow?

    A stack is a linear structure that follows last in, first out (LIFO).

  24. What does the top position represent in a stack?

    A stack’s top identifies the element available for the next stack operation.

  25. What do push, pop, and peek do on a stack?

    Push adds an element to the top of a stack; pop removes the top element; peek returns the top element without removing it.

  26. What is the typical time complexity of stack push, pop, and peek?

    With a tracked top position, push, pop, and peek can each take constant time, O(1)O(1)O(1), in a typical stack implementation.

  27. How can an array-based stack represent that it is empty?

    A stack is empty when it contains no elements; in an array-based implementation, a sentinel top value such as −1-1−1 may represent this state.

  28. What ordering principle does a queue follow?

    A queue is a linear structure that follows first in, first out (FIFO).

  29. At which ends does a queue insert and remove elements?

    A queue inserts elements at the rear and removes them from the front.

  30. What are the basic queue operations?

    Enqueue inserts an element, dequeue removes the front element, and peek operations inspect an end without removing its element.

  31. Why does a queue track front and rear positions?

    A queue keeps track of its front and rear positions; these positions help identify where removal and insertion occur.

  32. What conditions should an array-based queue check before enqueueing or dequeueing?

    In an array-based queue, inserting when the queue is full must be prevented; deleting when it is empty must also be prevented.

  33. Give examples of operating-system scheduling uses of queues.

    Queues are used in operating-system scheduling, including FIFO, round-robin, and multilevel queue approaches.

  34. Name two applications of queues besides basic waiting lines.

    Queues can support playlist ordering and the handling of processes or interrupts waiting for service.

  35. What is a linked list?

    A linked list is a linear structure whose nodes can occupy non-contiguous memory locations and are connected by references.

  36. What does a singly linked-list node contain?

    A singly linked-list node stores data and a reference to the next node; the final node’s next reference is typically null.

  37. What is the head of a linked list, and why must it be preserved?

    The head references the first node. Losing or overwriting it can make the list inaccessible.

  38. How does linked-list capacity differ from array capacity?

    A linked list can grow or shrink as nodes are added or removed, so its size is not fixed in advance.

  39. How can linked lists use memory when free space is fragmented?

    Linked lists can use scattered free memory locations, avoiding the need for one contiguous block.

  40. Why can insertion and deletion be easier in a linked list than in an array?

    Linked-list insertion or deletion can be performed by changing references, often without shifting every subsequent element.

  41. Why is random access inefficient in a linked list?

    A standard linked list does not provide direct indexed access; reaching a node generally requires traversing from a known node, often the head.

  42. Compare traversal in singly and doubly linked lists.

    A singly linked list’s links support forward traversal; a doubly linked list has previous and next links, supporting traversal in both directions.

  43. What do the outer links of a conventional doubly linked list contain?

    In a doubly linked list, the first node’s previous link and the last node’s next link are typically null.

  44. What makes a linked list circular?

    In a circular linked list, the last node links back to the first node instead of ending with a null next link.

  45. When can a linked list be a suitable choice?

    Linked lists are useful when memory is fragmented or when an application frequently inserts and deletes elements.

  46. What is a tree data structure?

    A tree is a non-linear, hierarchical structure made of nodes connected by edges.

  47. What are a tree’s root, parent, and child nodes?

    The root is the top node of a tree and has no parent; a parent’s directly connected lower nodes are its children.

  48. Define sibling nodes and leaf nodes.

    Sibling nodes share the same parent; a leaf node has no children.

  49. Compare the child restrictions of a general tree and a binary tree.

    A general tree node may have any number of children, whereas a binary-tree node has at most two: a left child and a right child.

  50. What ordering property defines a binary search tree?

    A binary search tree (BST) orders values so that values in a node’s left subtree are smaller and values in its right subtree are greater, according to the BST ordering rule.

  51. How is a value inserted into a binary search tree?

    To insert into a BST, compare the value with the current node and continue left for a smaller value or right for a greater value until an available position is found.

  52. How does BST ordering help guide a search?

    BST searching compares the target with the current node and follows only the left or right subtree indicated by the comparison.

  53. What are the main cases to consider when deleting a node from a BST?

    BST deletion differs by case: a leaf can be removed directly; a node with one child can be replaced by that child; a node with two children needs a replacement that preserves the BST ordering.

  54. What is the visit order for preorder traversal?

    Preorder traversal visits root, then left subtree, then right subtree.

  55. What is the visit order for inorder traversal?

    Inorder traversal visits left subtree, then root, then right subtree.

  56. What is the visit order for postorder traversal?

    Postorder traversal visits left subtree, then right subtree, then root.

  57. What order does inorder traversal produce for a BST?

    Inorder traversal of a BST visits its values in sorted order, assuming the BST ordering property is maintained.

  58. Give two uses of preorder traversal.

    Preorder traversal can be used to serialize or copy a tree and to produce prefix notation from an expression tree.

  59. Give two uses of postorder traversal.

    Postorder traversal can be useful when deleting a tree because children can be handled before their parent; it can also produce postfix notation from an expression tree.

  60. What do time complexity and space complexity measure?

    Time complexity describes how the number of basic operations grows with input size; space complexity describes how memory requirements grow.

  61. What do best-case, average-case, and worst-case analysis describe?

    Best case is the most favorable input, average case describes typical performance, and worst case is the least favorable input and gives an upper-bound style measure.

  62. How does linear search find a target in an array?

    Linear search checks elements one by one and stops when it finds the target or reaches the end.

  63. What are the best- and worst-case time complexities and auxiliary space of iterative linear search?

    Linear search takes O(1)O(1)O(1) time in the best case and O(n)O(n)O(n) time in the worst case; its auxiliary space is O(1)O(1)O(1) in the described iterative approach.

  64. What results can a linear-search method return?

    If linear search finds a target, it can return its index; if it exhausts the array without finding it, it can return a not-found result such as −1-1−1.

  65. How does binary search reduce its search range?

    Binary search repeatedly compares the target with a middle element and discards the half that cannot contain the target.

  66. What key precondition lets binary search discard half the search range?

    Binary search requires a sorted array or another ordering that allows one half to be safely ruled out after each comparison.

  67. What is binary search’s worst-case time complexity, and why?

    Binary search runs in O(log⁡n)O(\log n)O(logn) time in the worst case because each step roughly halves the remaining search range.

  68. How can the auxiliary-space requirements differ between iterative and recursive binary search?

    An iterative binary search can use O(1)O(1)O(1) auxiliary space; a recursive implementation uses additional call-stack space, typically O(log⁡n)O(\log n)O(logn) for a balanced halving search.

  69. How can you estimate complexity for sequential statements, nested loops, and alternative branches?

    For sequential statements, add their costs; for nested loops, multiply iteration counts; for alternative branches, use the cost of the branch that dominates the worst case.

  70. What time complexities do one nnn-iteration loop and two nested nnn-iteration loops generally contribute?

    A loop that runs once per input element generally contributes O(n)O(n)O(n) time; two nested loops each running nnn times generally contribute O(n2)O(n^2)O(n2).

  71. Why can recursion increase an algorithm’s space complexity?

    A recursion call can create an activation record on the call stack; recursive algorithms therefore may use additional space even when no explicit auxiliary array is declared.

  72. What is the difference between an in-place sort and a stable sort?

    A comparison sort’s in-place property concerns whether it needs auxiliary storage; stability concerns whether equal-key elements retain their relative order.

  73. What are the main phases of merge sort?

    Merge sort repeatedly divides the input into smaller parts, sorts them, and merges sorted parts into a complete sorted result.

  74. What is merge sort’s time complexity in its best, average, and worst cases?

    Merge sort has O(nlog⁡n)O(n\log n)O(nlogn) time complexity in the best, average, and worst cases.

  75. How does merge sort combine two sorted subarrays?

    The merge step compares the front elements of two sorted subarrays, copies the smaller one into the output, and then appends any remaining elements.

  76. What are the typical auxiliary-space and stability properties of merge sort?

    A typical array-based merge sort uses O(n)O(n)O(n) auxiliary space and is stable when equal elements are merged in their original relative order.

  77. What are the main steps of quicksort?

    Quicksort chooses a pivot and partitions the array so values fall on the appropriate sides of the pivot; it then recursively sorts the partitions.

  78. What is the goal of a quicksort partition step?

    A partition operation places the pivot in its final sorted position and separates values that are smaller from those that are greater, according to the chosen partition rule.

  79. What are quicksort’s average- and worst-case time complexities?

    Quicksort has average-case time complexity O(nlog⁡n)O(n\log n)O(nlogn) and worst-case time complexity O(n2)O(n^2)O(n2), which can occur with highly unbalanced partitions.

  80. What does a greedy algorithm do, and what limitation can it have?

    A greedy algorithm makes a locally best choice at each step. This can produce a feasible solution, but does not guarantee a globally optimal one for every problem.

  81. Name three problems or algorithms that use greedy choices.

    Greedy methods are used in problems such as fractional knapsack, Dijkstra’s shortest-path algorithm, and minimum spanning tree algorithms such as Prim’s and Kruskal’s.

  82. How do fractional and 0/1 knapsack differ?

    The fractional knapsack problem allows an item to be taken partially; the 0/1 knapsack problem requires each item to be either taken whole or not taken.

  83. What is the greedy strategy for the fractional knapsack problem?

    For fractional knapsack, calculate each item’s value-to-weight ratio, consider items in descending ratio order, and take a fraction of the next item if needed to fill the capacity.

  84. Contrast the central decision strategy of greedy algorithms and dynamic programming.

    Dynamic programming uses solutions to overlapping subproblems to build a global solution; a greedy method commits to locally best choices without generally reconsidering them.

  85. What are the basic components of a graph?

    A graph consists of vertices (nodes) and edges that represent relationships or connections between vertices.

  86. Compare directed and undirected graphs.

    A directed edge has a specified direction; an undirected edge represents a connection with no direction.

  87. What distinguishes a connected graph from a disconnected graph?

    A connected graph has paths linking its vertices; a disconnected graph has separated components or an isolated vertex.

  88. How does an adjacency matrix represent a graph?

    An adjacency matrix records whether each pair of vertices is connected, typically using rows and columns for vertices and entries such as 111 and 000.

  89. How does an adjacency list represent a graph?

    An adjacency list stores, for each vertex, a list of the vertices directly adjacent to it.

  90. How does BFS traverse a graph, and which data structure does it typically use?

    Breadth-first search (BFS) visits vertices level by level, typically using a queue and a visited marker to avoid revisiting vertices.

  91. How does DFS traverse a graph, and what supports its backtracking?

    Depth-first search (DFS) follows a path as far as possible before backtracking; it can be implemented with a stack or recursion and uses visited markers.

  92. What properties define a spanning tree of a connected graph?

    A spanning tree connects every vertex of a connected graph without cycles and contains V−1V-1V−1 edges for VVV vertices.

  93. What is a minimum spanning tree?

    A minimum spanning tree is a spanning tree whose total edge weight is as small as possible.

  94. How does Prim’s algorithm build a minimum spanning tree?

    Prim’s algorithm grows a spanning tree from a starting vertex by repeatedly choosing a minimum-weight edge that connects the current tree to an unvisited vertex.

  95. How does Kruskal’s algorithm build a minimum spanning tree?

    Kruskal’s algorithm considers edges in increasing weight order and adds an edge when it does not create a cycle, until a spanning tree is formed.

  96. What is the main difference between Prim’s and Kruskal’s approaches?

    Prim’s algorithm expands from a chosen starting vertex, while Kruskal’s algorithm selects the globally lightest eligible edges without requiring one starting vertex.

Card 1 of 96
Card 1 of 96

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent