Leap

DSA problems practice notes

· Sai Kiran

Concept-wise

Array based

Given an array of integers find the subarrays with sum K

Refer https://www.geeksforgeeks.org/find-subarray-with-given-sum-in-array-of-integers/ or https://www.geeksforgeeks.org/number-subarrays-sum-exactly-equal-k/ they are about the same problem.

Brute force

Generate all the subarrays, calculate sum and check. N-choose-2 possibile subarrays will be there.

Better approach

While traversing the array, track the current sum, store the sum of array ending at each index in a hash map (sum and index mapping). Check if the K-currsum exists in the hashmap.

Number of substrings having an equal number of lowercase and uppercase letters

Refer Number of substrings having an equal number of lowercase and uppercase letters It can be reduced to the above subarrays with sum K, where K=0 and each upper case letter is considered 1 and lower case letter is considered -1. Reference: https://www.geeksforgeeks.org/number-of-substrings-having-an-equal-number-of-lowercase-and-uppercase-letters/

Fast and slow pointers

  • Classify the number in an array: Move all the odd numbers to beginnig of the array/linked list
  • Remove zeros from the array/linked list
  • Remove duplicate numbers from the array/lined list
  • Move Zeros to the end
  • Check if loop exists in the given linked list

Two pointers

Misc

Dynamic Programming

Important points: Break down the problem, write a recurrence that solves the problem. There are two approaches:

Problems to practice

Queries on a stream of data

Previous questions and learnings

Attempt 1

  • Given a set of cartesian points, generate the bounding box that includes all the points.
  • Given two strings, and we can cut the strings at same position. After cut we take the left half of the first string and right part of the second string and join them to form a new string. How many of those new strings are plindromes - https://cs.stackexchange.com/questions/109662/divide-two-strings-to-form-palindrome
  • Given a string with limited number of characters repeating. Find the number of substrings where the count of all allowed characters is same in the substring.
  • General json validation: string processing. Should have covered all of the edge cases. And validated the approached up front. So that I was confident while coding.
  • Given start binary pattern and destination binary pattern, see if we can reach from start pattern to end, in each step only one bit can be flipped. Also given a set of safe states through which you should be traversing. Moving to a non-safe pattern in invalid. https://leetcode.com/discuss/interview-experience/515564/google-l3-hyderabad-feb-2020-rejected lock round 5
  • Given a dictionary of words, suggest the user words based first few characters typed, like the functionality in mobile phone typing scenario.

Attempt 2

  • A robot is emitting stream of messages, the format of the messages is: MSG TIMESTAMP. Our goal is to only log the events that are not repeated within K(for ex:10) units of time. Robot is emitting messages - time - message.
    • Solution: A simple hashing solution where, we store the message and its last seen timestamp in hash map, next time if the same message is seen, we will check the diff of timestamps. If it is less than K(for ex:10) then we show it.
  • Searialize standard python objects into JSON.
  • Bigram model
    • Question
    • My solution
    • The question was easy, I was supposed to finish this quickly so that a little difficult followup could be asked.
    • Also, during interview I wanted to get the pair with maximum value, from hashmap quickly, but I couldn’t get effective one other than O(n). Check the above code I wrote, I could have use collections.Counter in place of dict and may be could have use most_common method, but time complexity wise no better than linear. Because most_common takes lower bounds of sorting amount of time, i.e, O(nlogn) when argument isn’t specified. And, they used heap data structure when argument is specificed, this is like creating heap and performing n pops.
    • Learning: Be confident, no self-doubt.
    • We wanted a sorted set kind of thing.

Ap-attempt1

Shaw-attempt1

  • Hacker rank test questions
    • Q1
    • Q2