Hashmap

Design Twitter

January 26, 2024
medium
Hashmap

Problem Statement # Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and check the 10 most recent tweets in the user’s news feed. Functionalities: # postTweet(userId, tweetId): Compose a new tweet. getNewsFeed(userId): Retrieve the 10 most recent tweet IDs in the user’s news feed. Each item in the news feed must be posted by users who the user followed or by the user themself. ...

Pair of Songs With Total Durations Divisible by 60

January 26, 2024
medium
Hashmap

Problem Statement # You are given a list of songs where the i-th song has a duration of time[i] seconds. Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0. Example: Input: time = [30, 20, 150, 100, 40] Output: 3 Explanation: Three pairs have a total duration divisible by 60: (time[0] = 30, time[2] = 150): total duration 180 (time[1] = 20, time[3] = 100): total duration 120 (time[1] = 20, time[4] = 40): total duration 60 Solution Approach # The solution involves using a hashmap to keep track of the frequencies of song durations modulo 60. ...

Balanced Substring Checker

January 14, 2024
medium
Hashmap

Problem # Given a string s consisting of only two characters A and B, find the length of the longest substring that contains an equal number of As and Bs. Example: # Input: s = "ABABBAABB" Output: 8 (The longest balanced substring is “ABABBAAB”) Solution Approach: # The solution involves using a hash map to keep track of the counts of As and Bs and their differences at each index. ...

Continuous Subarray Sum

January 14, 2024
medium
Hashmap

Problem # Given an array of non-negative integers nums and an integer k, find if the array has a continuous subarray of size at least two that sums up to a multiple of k. Example: Input: nums = [23, 2, 4, 6, 7], k = 6 Output: True Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6. Solution Approach # The solution uses a hashmap to store the remainder of the cumulative sum modulo k and their indices. ...

Subarray with Equal Occurrences

January 11, 2024
medium
Hashmap

Problem # Given an array of integers nums and two integers x and y, find the length of the longest contiguous subarray where the number of occurrences of x is equal to the number of occurrences of y. Example: Input: nums = [1, 2, 2, 3, 1, 1, 3], x = 1, y = 3 Output: 6 Explanation: The longest subarray with equal occurrences of 1 and 3 is [2, 2, 3, 1, 1, 3]. ...