coding interview concepts Flashcards
Describe the difference between singly and doubly linked lists.
- Singly linked list: Each node contains data and a reference to the next node. \n* Doubly linked list: Each node contains data, a reference to the next node, and a reference to the previous node.
Doubly linked lists allow for easier traversal in both directions compared to singly linked lists.
What is a stack and how does it differ from a queue?
Stacks operate on the LIFO principle, while queues operate on the FIFO principle.
LIFO: Last In, First Out; FIFO: First In, First Out. Stacks are used for backtracking, recursion, etc., while queues are used for scheduling, order processing, etc.
Explain what a binary search tree is.
A tree data structure where each node has at most two children, and every node to the left is less than the parent node, while every node to the right is greater.
Binary search trees are used for efficient searching and sorting.
Define hash tables and their primary use.
A data structure that maps keys to values for highly efficient lookup.
Hash tables use a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.
What are tries and where are they typically used?
A tree-like data structure used to store a dynamic set or associative array where the keys are usually strings.
Tries are commonly used in searching applications, like autocomplete and spell checking.
Describe the two ways of representing graphs in data structures.
- Adjacency List: Represents a graph as an array of lists. \n* Adjacency Matrix: Represents a graph as a 2D array of vertices.
Adjacency lists are more space-efficient for sparse graphs, while adjacency matrices provide faster access for dense graphs.
What is the time complexity of Bubble Sort in the worst case?
O(n^2)
Bubble Sort is less efficient on large lists, as it compares all the elements one-by-one and swaps them if they are in the wrong order.
Define the concept of recursion in programming.
A method of solving a problem where the solution involves solving smaller instances of the same problem.
Recursion often simplifies a complex problem and can be used in sorting, searching algorithms, and more.
What does Big O notation generally describe?
The upper limit of the time or space complexity of an algorithm as the size of the input data increases.
Big O notation helps in understanding the efficiency and scalability of an algorithm.
What is a greedy algorithm and its typical use?
An algorithm that makes the locally optimal choice at each stage with the hope of finding a global optimum.
Greedy algorithms are used in optimization problems and often provide a good approximation for complex problems.
How does merge sort work?
An algorithm that divides the array into halves, sorts each half, and then merges them back together.
Merge sort has a time complexity of O(n log n) and is efficient for large datasets.
What is the primary application of the two pointers technique?
Used to scan an array or sequence from two ends to find a pair or calculate a sum.
This technique is efficient for problems involving sequences, such as finding a pair with a given sum.
Explain the principle of Divide and Conquer.
An algorithm design paradigm that works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly.
Commonly used in algorithms like quicksort, mergesort, and binary search.
What is a binary heap and its common use?
A complete binary tree that satisfies the heap property, used primarily in priority queues.
In a max heap, each parent node is greater than its child nodes, while in a min heap, each parent node is smaller.
Describe the function of Dijkstra’s Algorithm.
An algorithm for finding the shortest paths between nodes in a graph.
Dijkstra’s Algorithm is used for graph traversal and pathfinding in networks.
What does Big O notation express about an algorithm’s space complexity?
The total amount of memory space that an algorithm needs to run, in terms of the size of the input data.
Space complexity is crucial for evaluating the efficiency of an algorithm in terms of memory usage.
Define the concept of backtracking in algorithms.
A form of recursion where you try to build a solution incrementally and abandon a path as soon as it is determined that this path cannot possibly lead to a solution.
Backtracking is used in problems like puzzle solving, game theory, and more.
How do hash table lookups work?
By using a hash function to compute an index where a value is stored in a data structure.
Hash tables provide constant time complexity for search, insert, and delete operations in the average case.
What is the main idea behind the sliding window technique?
A method of tracking a subset of data within a larger set by maintaining a window that slides over the data to examine different sections of it.
Typically used in array or string problems to find or calculate something among all contiguous subarrays or substrings of a given size.
Explain the concept of linear search.
A method of finding a specific value in a list that checks each element in sequence until the desired element is found or the list ends.
Linear search is simple but inefficient compared to other searching algorithms for large lists.
What is the core principle of dynamic programming?
A method of solving complex problems by breaking them down into simpler subproblems, storing the results of these subproblems to avoid computing the same results again.
Used in a wide range of applications, from computer algorithms to mathematical problems.
Describe the binary search algorithm.
A method of finding an item in a sorted list by repeatedly dividing the search interval in half.
The key condition for binary search is that the array must be sorted beforehand.
What are the basic principles of object-oriented programming (OOP)?
Encapsulation, Abstraction, Inheritance, and Polymorphism.
OOP focuses on using objects that are instances of classes, which can contain data and methods.
How is a graph represented using an adjacency list?
Each node of the graph is represented as a list, with the list containing the adjacent nodes that are directly connected to it.
Adjacency lists are preferred for representing sparse graphs, as they are more space-efficient.