46 data structures and algorithms questions46 Interview Questions
Simulate a simple bank system that supports basic operations like deposit, withdraw, and check balance for different accounts. You will implement methods to handle these transactions and ensure validity based on account balances and existence.
#1. Simple Bank System
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.
#2. Text Justification
Simulate the Candy Crush game. Repeated sequences of the same candy type (3 or more) should be removed until no more such sequences exist.
#3. Candy Crush
You are given a list of 24-hour clock times. Find the smallest difference in minutes between any two times in the list.
#4. Minimum Time Difference
Traverse a given matrix in a spiral pattern, returning all elements in the order you visit them. Essentially, walk around the matrix layer by layer, adding elements to a list.
#5. Spiral Matrix
You are given a grid and a series of queries to place blocks of varying sizes. Determine if each block placement is valid given existing blocks and the grid boundaries.
#6. Block Placement Queries
Imagine a grid where each cell has a value. Find the fewest changes needed to make the grid look like the letter 'Y', where only specific cells can be modified.
#7. Minimum Operations to Write the Letter Y on a Grid
Determine how many subarrays within a given array have alternating positive and negative numbers. The subarrays must have a minimum length of two.
#8. Count Alternating Subarrays
You are given a square matrix representing an image. Rotate the image by 90 degrees clockwise in-place.
#9. Rotate Image
You're given a list of numbers. For each number, if it has exactly four divisors, find the sum of those divisors; then, return the overall sum of these divisor sums.
#10. Four Divisors
Given an absolute file path (Unix style), simplify it by resolving redundant separators, directory names, and navigating back up the directory structure. Return the simplified canonical path.
#11. Simplify Path
Repeatedly subtract the smaller of two numbers from the larger until one of them becomes zero. Count how many subtractions it takes to reach this state.
#12. Count Operations to Obtain Zero
Find the longest continuous subarray where the absolute difference between any two elements is less than or equal to a given limit. Return the length of this subarray.
#13. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
You're given a message and a limit on the length of each part. Divide the message into parts, adding a suffix like "(1/5)" to each, ensuring each part respects the length limit.
#14. Split Message Based on Limit
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.
#15. Rotating the Box
You are given a list of strings. Count how many pairs of strings exist in the list where one string is both a prefix and a suffix of the other string.
#16. Count Prefix and Suffix Pairs II
Find the longest common prefix string amongst an array of strings. Return the length of this common prefix.
#17. Find the Length of the Longest Common Prefix
Imagine a grid where some 2x2 blocks are marked. Count how many unique integers from 0 to 4 (inclusive) represent the number of marked cells within those 2x2 blocks that intersect with given coordinates.
#18. Number of Black Blocks
Find the largest rectangular area that can fit within a histogram defined by an array of bar heights. You are given an array representing the height of bars, where each bar has a width of 1.
#19. Largest Rectangle in Histogram
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.
#20. Number of Flowers in Full Bloom