Open In App

Contest Experience – GeeksForGeeks Weekly Coding Contest 119

It was a weekly coding contest from GEEKS FOR GEEKS There were 3 coding questions. The First question was of 15 marks the second question was of 25 marks and the last one was of 4 marks in total it was of 100 marks.

Contest Description



GeeksforGeeks organizes a weekly recurring contest series that is designed to simulate the coding interview rounds of tech giants such as Google, Amazon, Adobe, Paytm, etc. In this, we need to solve a given number of questions under 1.5 hours. Generally, they are held on Sundays of every week from 7:00 pm to 8:30pm

Question 1: High Frequency



You are given a string S of lowercase Latin characters with size N. Your task is to find the lexicographically smallest substring with the maximum frequency.
Note: String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied that pj = qj. For example, abc is lexicographically smaller than abcd, abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.

Example

Input: N = 4, S = “gfgg”

Output: “g”

Explanation: Substring “g” is present 3 in the string and it can be proved that it is the lexicographically smallest one

Solution




class Solution:
    def solve(self, N, S):
 
        # code here
        char_freq = {}
 
        max_freq = 0
        smallest_char = None
 
        for i in range(N):
            char = S[i]
 
            if char in char_freq:
                char_freq[char] += 1
            else:
                char_freq[char] = 1
 
            if char_freq[char] > max_freq or (char_freq[char] == max_freq and (smallest_char is None or char < smallest_char)):
                max_freq = char_freq[char]
                smallest_char = char
 
        result = smallest_char
        return result

Question 02: Plucking Flowers

As Women’s Day is coming, Geek decided to give one flower to each woman he knows.

Geek knows K women. Geek’s neighbour has a garden in which there are N flowers and each flower has a beauty associated with it i.e. ith flower has B[i] beauty. Geek wants to pluck exactly K flowers from his neighbour’s garden such that the sum of beauties of all flowers he plucked is maximum but to not get caught he can’t pluck more than one flower in a row, more formally he can’t pluck two flowers if they are adjacent.

Your task is to find maximum sum of beauty of all flowers Geek can get.

Example

Input: N = 3 K = 2, B[] = {1, 1000000, 1}

Output: 2

Explanation: Geek has only one option i.e. plucking first and third flower.

Solution:




class Solution:
    def maximumBeauty(self, n, k, lst):
 
        matrix = [[0 for j in range(n+1)] for i in range(k+1)]
        for i in range(1, k+1):
            for j in range(2*(i)-1, n+1):
                if i == 1:
                    matrix[i][j] = max(matrix[i][j-1], lst[j-1])
                    continue
                matrix[i][j] = max(matrix[i][j-1], lst[j-1]+matrix[i-1][j-2])
        return matrix[-1][-1]

Question 03: Thrindrome

A Thrindrome is a palindromic sequence of length three.

A tree has N nodes numbered 0, 1,… N – 1. The structure of the tree is given by array p[ ] of size N where p[i] is the direct ancestor of ith node and p[0] = -1 as 0 is the root of the tree. Also, you are given a string C of size N, where C[i] represents the character written on the ith node.

Find the number of triplets of nodes (a, b, c){a≠ b≠ c} such that node b comes in the simple path from node a to c and C[a], C[b], and C forms Thrindrome.

Example:

Input: N = 5, p[] = {-1, 0, 0, 1, 1}, C = “accaa”

Output: 8

Explanation:

Tree looks like:

0’a’

/ \

1’c’ 2’c’

/ \

3’a’ 4’a’

Possible triplets are

(1, 0, 2), (2, 0, 1),

(3, 1, 0), (4, 1, 0),

(0, 1, 4), (0, 1, 3),

(3, 1, 4), (4, 1, 3)

The questions of this week I personally found them from medium to hard . The last question THRINDROME I think was the toughest one . If you are able to solve these question under the given time slot it means you are having a good catch on DSA topics.

Experience with problems

For the first question It took me around 10 minutes for the second one It took me around 20 to 25 minutes and talking about the last one I was unable to solve it after looking into it for straight 30 minutes. These questions were really new to me and I have never encountered these kind of problems .

Result

So I was able to solve first two problems. the third one I tried around 30 minutes but It was not done .So I ended up submitting these 2 problems and got around 58.75 points and a overall rank of 384 among around 3000 people.

It was my first coding contest experience and it was very cool . I learned a lot of things from this coding contest and wanna participate more and more.

The GFG weekly coding contest is one of the best place to practice your things and get good amount of exposure. It will not only help you to learn new things but also it will help you to boost your confidence ,So participate whenever you are free the timings are every sunday from 7 to 8:30 pm.

I solved 2 out of 3 questions and got around 58.75 points and a overall rank of 384 among around 3000 people.


Article Tags :