To detect a cycle in a linked list, you can use Floyd's Cycle-Finding Algorithm, also known as the hare and tortoise algorithm. Use two pointers, slow and fast, starting at the head of the list. Move slow one step at a time and fast two steps at a time. If they ever meet, there is a cycle; if fast reaches the end of the list, there is no cycle.
To reverse a linked list, you can iterate through the list and reverse the links between the nodes. Initialize three pointers: previous, current, and next. Traverse the list and at each node, reverse the link by setting current.next to previous. Move the pointers one step forward and repeat until the entire list is reversed.
To implement a binary search algorithm, start with the initial search interval covering the entire array. Check the middle element of the interval. If the middle element is the target, return its index. If the target is less than the middle element, repeat the search on the left half. If the target is greater, repeat on the right half. Continue until the target is found or the interval is empty.
To find the longest common subsequence (LCS) of two strings, use dynamic programming. Create a 2D array to store the lengths of LCS for substrings. Initialize the first row and column to 0. For each pair of characters from the two strings, if they match, set the cell to the diagonal value plus one. Otherwise, set it to the maximum of the cell above and the cell to the left. The bottom-right cell contains the length of the LCS.
To find the kth largest element in an array, you can use a min-heap. Build a min-heap of the first k elements of the array. Then iterate through the remaining elements, and for each element, if it is greater than the root of the heap, replace the root with the element and heapify. The root of the heap will be the kth largest element.
To solve the 0/1 Knapsack problem, use dynamic programming. Create a 2D array where the rows represent items and the columns represent weights from 0 to the maximum capacity. Initialize the first row and column to 0. For each item, if its weight is less than or equal to the current capacity, set the cell to the maximum of including the item (value of the item plus value of the remaining capacity) or excluding the item (value of the previous item at the same capacity). The bottom-right cell contains the maximum value that can be obtained.
To merge two sorted arrays, create a new array to hold the merged result. Use two pointers to track the current element of each input array. Compare the elements pointed to by the pointers, append the smaller element to the result array, and move the pointer of the array from which the element was taken. Repeat until all elements are merged.
To find the first non-repeating character in a string, use a hash table to store the frequency of each character. Iterate through the string to fill the hash table. Then iterate through the string again and return the first character with a frequency of one. If no such character exists, return a special value indicating so.
To check if two strings are anagrams, sort both strings and compare them. If they are identical, the strings are anagrams. Alternatively, you can use a hash table to count the frequency of each character in both strings and compare the counts. If all character counts match, the strings are anagrams.
To implement a breadth-first search (BFS) algorithm, use a queue to explore the graph level by level. Initialize the queue with the starting node and mark it as visited. While the queue is not empty, dequeue a node, process it, and enqueue its unvisited neighbors, marking them as visited. Repeat until all reachable nodes are processed.
A hash table is a data structure that maps keys to values using a hash function. It provides efficient data retrieval by computing an index from the key's hash code, allowing for quick lookup, insertion, and deletion operations. Common issues include handling collisions through chaining or open addressing.
The two-pointer technique involves using two pointers to iterate through an array or list, often used to solve problems related to pairs or subarrays. It can optimize time complexity by reducing nested loops, and is commonly used in problems like finding pairs with a given sum or reversing a sublist.
A trie is a tree-like data structure that stores a dynamic set of strings, where the keys are usually strings. It is used for efficient retrieval of a key in a dataset of strings, such as autocomplete and spell checker applications.