Posts

What I Learned from Solving 100 HackerRank Problems

I started HackerRank in November 2020 during college for coding practice. After 2 years and 100+ solved problems, here is what I learned, and what I wish I’d known at problem one. 1. Reading the problem carefully is half the solution Many of my failed submissions came from misreading the input format, the constraints, or the expected output, not from wrong logic. Now I read the sample input and output first, then the constraints, then the description. 2. Constraints tell you which approach will work The Array Manipulation problem showed this clearly. My first solution looped through every range for every query. It worked on small inputs and timed out on large ones. The fix was to record only the start and end of each range, then add everything up in one pass: def arrayManipulation(n, queries): diff = [0] * (n + 2) for a, b, k in queries: diff[a] += k diff[b + 1] -= k best = cur = 0 for x in diff: cur += x best = max(best, cur) ...

Mistakes I Made as a Beginner Programmer (and How to Avoid Them)

When I started learning to code at college, I thought the hard part would be the syntax. It wasn’t. The hard part was the habits I built without realizing they were slowing me down. Here are the mistakes that cost me the most time, and what I’d do differently. 1. Copying solutions instead of solving problems On my first HackerRank problems, I looked up the answer after 5 minutes of getting stuck. It felt productive, since the problem showed as “solved”. But a few days later I couldn’t solve a similar problem on my own. What to do instead: Give yourself a fixed struggle time (say 30 minutes) before looking at hints. If you do read a solution, close it and rewrite it from memory the next day. If you can’t, you didn’t learn it yet. 2. Jumping between courses and tutorials Starting a new course feels like progress, but finishing none of them means you only ever learn chapter one. What to do instead: Pick one resource and finish it. Build something small after each section, eve...

ginortS in Python - HackerRank solution

Solution for hackerRank problem ginortS in Python Problem : You are given a string S.  S contains alphanumeric characters only.  'SORTING' Your task is to sort the string S in the following manner: All sorted lowercase letters are ahead of uppercase letters. All sorted uppercase letters are ahead of digits. All sorted odd digits are ahead of sorted even digits. Input Format A single line of input contains the string S. Constraints 0 < len(S) < 1000 Output Format Output the sorted string S. Sample Input Sorting1234 Sample Output ginortS1324 Code : s = str(input()) Upper = [] lower = [] odd_no = [] even_no = [] for i in s:     if(i.isupper()):         Upper += [i]     if(i.islower()):         lower += [i]     if(i.isnumeric()):         if(int(i)%2 == 0):             even_no += [i]         else :           ...

Arrays - DS in Python - HackerRank solution

Image
Solution for hackerRank problem Arrays-DS in Python Problem : An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A , of size N, each memory location has some unique index,  i(where 0 <= i < N), that can be referenced as A[i] or Ai. Reverse an array of integers. Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this. Example A = [1, 2, 3] Return [3, 2, 1]. Function Description Complete the function reverseArray in the editor below. reverseArray has the following parameter(s): int A[n]: the array to reverse Returns int[n]: the reversed array Input Format The first line contains an integer, N, the number of integers in A. The second line contains N space-separated integers that make up A. Constraints 1 <= N <= 10^3 1 <= A[i] <= 10^4, where A[i] is the ith integer in A Sample Input 1 Sample Output 1 2 3 4 1 Code : #!/bin/python3 import math impor...

Time Delta in Python - HackerRank solution

Solution for hackerRank problem Time Delta in Python Problem : When users post an update on social media,such as a URL, image, status update etc., other users in their network are able to view this new post on their news feed. Users can also see exactly when the post was published, i.e, how many hours, minutes or seconds ago. Since sometimes posts are published and viewed in different time zones, this can be confusing. You are given two timestamps of one such post that a user can see on his newsfeed in the following format: Day dd Mon yyyy hh:mm:ss +xxxx Here +xxxx represents the time zone. Your task is to print the absolute difference (in seconds) between them. Input Format The first line contains T, the number of testcases. Each testcase contains  2 lines, representing time t1 and time t2. Constraints Input contains only valid timestamps year <= 3000 Output Format Print the absolute difference (t1-t2)  in seconds. Sample Input 0 2 Sun 10 May 2015 13:54:36 -0700 Sun 10 May...

Validating Credit Card Numbers in Python - HackerRank solution

Solution for hackerRank problem Validating Credit Card Numbers in Python Problem : You and Fredrick are good friends. Yesterday, Fredrick received  N credit cards from ABCD Bank. He wants to verify whether his credit card numbers are valid or not. You happen to be great at regex so he is asking for your help! A valid credit card from ABCD Bank has the following characteristics: ► It must start with a  4, 5 or 6. ► It must contain exactly  16 digits. ► It must only consist of digits (0-9). ► It may have digits in groups of 4, separated by one hyphen "-". ► It must NOT use any other separator like ' ' , '_', etc. ► It must NOT have  4 or more consecutive repeated digits. Examples: Valid Credit Card Numbers 4253625879615786 4424424424442444 5122-2368-7954-3214 InValid Credit Card Numbers 42536258796157867       #17 digits in card number → Invalid  4424444424442444        #Consecutive digits are repeating 4 or more times → Inv...

Array Manipulation in Python - HackerRank Solution

Image
Solution for hackerRank problem Array Manipulation in Python Problem : Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example, n = 10 queries = [ [1, 5, 3], [4, 8, 7], [6, 9, 1] ] Queries are interpreted as follows: Add the values of k between the indices a and b inclusive: The largest value is 10 after all operations are performed. Function Description Complete the function arrayManipulation in the editor below. arrayManipulation has the following parameters: int n - the number of elements in the array int queries[q][3] - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k. Returns int - the maximum value in the resultant array Input Format The first line contains two space-separated integers  n and  m, the size of the array and the number of ...