53 data structures and algorithms questions53 Interview Questions
Rearrange the characters in a given string such that no two adjacent characters are the same. If such an arrangement is possible, return it; otherwise, return an empty string.
#1. Reorganize String
You're given a list of tasks to run, each requiring one unit of time to complete. Determine the shortest time needed to execute all tasks, considering a cooling period after each task execution.
#2. Task Scheduler
You're given a row of seats, some occupied and some empty. Find the best seat to maximize the distance to the nearest person.
#3. Maximize Distance to Closest Person
Design a hit counter to track the number of hits recorded in the past 5 minutes. Implement methods to record a hit and retrieve the total hits in the past 5 minutes.
#4. Design Hit Counter
Simulate a browser's history navigation. Implement functionalities to visit URLs, go back in history, and go forward.
#5. Design Browser History
You are given a collection of intervals. Merge all overlapping intervals into a single interval and return the result.
#6. Merge Intervals
Given a sequence of words and a line width, format the text to fit within the specified width. You must justify the text, distributing extra spaces evenly between words.
#7. Text Justification
You're given a list where each person indicates the size of the group they need to be in. Your task is to form groups according to these sizes and return a list of these groups.
#8. Group the People Given the Group Size They Belong To
Find the number of subarrays within a given array that contain exactly k odd numbers. A subarray is considered "nice" if it meets this condition.
#9. Count Number of Nice Subarrays
You're given a linked list where each node contains a 0 or 1, representing a binary number. Convert this binary representation into its equivalent integer value.
#10. Convert Binary Number in a Linked List to Integer
Find the substring that occurs the most within a given string, subject to length and character limit constraints. Return the maximum number of times this substring appears.
#11. Maximum Number of Occurrences of a Substring
You are given positions of balls on a line. Maximize the minimum distance between any two balls when placing a given number of balls on these positions.
#12. Magnetic Force Between Two Balls
You're given a list of domain names with visit counts. Calculate and return the visit counts for each subdomain.
#13. Subdomain Visit Count
Convert a non-negative integer into its English word representation. For example, 12345 should be converted to 'Twelve Thousand Three Hundred Forty Five'.
#14. Integer to English Words
Determine if a string containing parentheses is valid. A valid string has matching and properly nested opening and closing parentheses.
#15. Valid Parentheses
Design a data structure that efficiently stores a set of strings and allows for prefix-based searches. Implement methods to insert strings, search for complete words, and check if any word starts with a given prefix.
#16. Implement Trie (Prefix Tree)
You are given a square matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
#17. Rotate Image
Simulate a box tilting to the right, causing stones to fall until they hit obstacles or the floor. Re-arrange the characters in a given grid to reflect the resulting state of the box.
#18. Rotating the Box
Imagine people entering and leaving a flower garden at different times. Given the arrival and departure times for each person, and the bloom period for each flower, determine how many flowers are in full bloom when each person is in the garden.
#19. Number of Flowers in Full Bloom
Given an array representing the height of bars, calculate how much water can be trapped between them after rain. Think about finding the maximum height to the left and right of each bar.
#20. Trapping Rain Water