Author: akshay
This problem uses binary search to find a hidden number between 1 and n using a feedback API guess(num), which tells us whether our guess is too high, too low, or correct. Steps Initialize two pointers: l = 1 and r = n. Use binary search to guess the middle number m. If guess(m) returns: 0 → m is the correct number -1 → the picked number is smaller → move left 1 → the picked number is larger → move right Repeat until the number is found. Dry Run Input: n = 10, pick = 6 Guess 5 →…
This problem is about computing the floor of the square root of a number x using an optimized binary search approach. A best practice in binary search is to calculate the mid-point in a way that avoids integer overflow: m = l + (r – l) / 2. Steps If x is less than 2, return x. Set search boundaries: l = 2, r = floor(x / 2). Calculate mid-point safely: m = l + floor((r – l) / 2). Compare m * m with x. If m * m == x, return m. If m * m > x,…
This problem is about finding the floor value of the square root of a non-negative integer x. You cannot use built-in square root functions. The goal is to use binary search to compute the answer efficiently. Steps If x is 0 or 1, return x. Set the binary search range: l = 2, r = floor(x / 2). Use binary search to find the greatest number m such that m * m ≤ x. If m * m === x, return m. If m * m > x, search left side. If m * m < x, search right side.…
Instead of sorting the string, this approach creates a unique hash key for each string based on the frequency of characters. This improves performance by avoiding costly sort operations for every string. Steps Initialize a hashmap to store grouped anagrams. For each word in the input array: Create an array of size 26 to count frequency of each letter. Convert that frequency array into a unique string key (like “#1#0#2…”). Use this string as a hash key to group anagrams. Return all the grouped values. Dry Run Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”] “eat” → freq = [1,0,0,…,1,1,…] →…
The goal is to group words that are anagrams of each other. An anagram is a word formed by rearranging the letters of another word. This approach uses a sorted version of each string as a unique key to group anagrams together. Steps Initialize a hashmap to store grouped anagrams. For each word in the input list: Sort the characters in the word alphabetically. Use the sorted string as a key. Add the original word to the list at that key. Return all grouped lists of anagrams from the hashmap. Dry Run Input: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”] Sorted…
Two strings s and t are isomorphic if the characters in s can be replaced to get t, maintaining a one-to-one mapping between the characters. No two characters may map to the same character but a character may map to itself. Steps Initialize two maps: one from s to t and the other from t to s. Traverse both strings simultaneously. If a mapping doesn’t exist in either direction, create it. If the mapping exists but doesn’t match the current characters, return false. If the loop completes without conflicts, return true. Dry Run Input: s = “egg”, t = “add”…
This problem checks whether two strings are anagrams of each other. Two strings are anagrams if they contain the exact same characters with the same frequency but possibly in a different order. Steps First, check if the lengths of both strings are equal. If not, return false. Create a hashmap (or character counter) to store the frequency of characters in the first string. Iterate over the second string and decrease the corresponding frequency in the map. If a character is not found or the count goes below zero, return false. If all characters match, return true at the end. Dry…
This problem focuses on finding the longest common prefix string shared among an array of strings. If no common prefix exists, the result should be an empty string. Steps Initialize a pointer x to track character positions in the first string. Iterate through each character of the first string using while loop. For every character at position x in the first string, compare it with the character at the same position in the other strings. If a mismatch is found or if the current index exceeds the length of any string, return the substring from the first string from 0…
The task is to find the largest-valued odd number that can be formed from a given numeric string by removing trailing even digits. The number must be a substring starting from index 0 to some valid index. Steps Start from the end of the string and move backward. Check if the current digit is odd using modulus % 2. If an odd digit is found, return the substring from index 0 to that digit (inclusive). If no odd digit exists in the string, return an empty string. Dry Run Input: “52” Start from the end: ‘2’ → even → skip…
This approach checks if a string is a valid palindrome by using two pointers. It ignores non-alphanumeric characters and is case-insensitive. A valid palindrome reads the same forward and backward after filtering. Steps Convert the string to lowercase to simplify comparison. Initialize two pointers: one at the start and one at the end of the string. Move both pointers toward the center while skipping non-alphanumeric characters. Compare the characters at both pointers. If they differ, return false. If they match, continue moving. If all valid characters match, return true. Dry Run Input 1: “race a car” Lowercase: “race a car”…
Contact Us
Subscribe to Stay Updated
Stay ahead in the world of tech with our exclusive newsletter! Subscribe now for regular updates on the latest trends, valuable coding resources, and tips to boost your frontend development skills.
