Round 1
Questions:
TikTok Viral Challenge
In the competitive world of social media, especially on platforms like TikTok, creators are constantly battling to produce viral content. The stakes are high—every video segment counts. As a data analyst working for TikTok, you've been tasked with a critical mission: to help creators optimize their videos by identifying the most promising segments that could lead to viral success.
Each segment of a TikTok video has an engagement attribute, which reflects its potential to go viral. This attribute is represented as an integer array engagementArray
of size 26, corresponding to the letters of the alphabet ('a' to 'z'). Each element in this array is either '1' (indicating a viral segment) or '0' (indicating a non-viral segment).
The goal is to count all possible viral content combinations. A TikTok video is considered viral if the number of non-viral segments in any substring of the video does not exceed a given threshold k
. The video is represented by a string of length n
.
Given:
- A string
video
of lengthn
. - An integer
k
, the maximum allowed number of non-viral segments in a viral TikTok video. - An integer array
engagementArray
of size 26, where each element is either '1' (viral) or '0' (non-viral).
Find the number of unique non-empty substrings of the video that qualify as viral content.
Input Format for Custom Testing: Input from stdin will be processed as follows and passed to the function.
- The first line contains a string,
video
. - The next line contains an integer 26, the size of the
engagementArray
. - Each of the next 26 lines contains an integer,
engagementArray[i]
. - The next line contains an integer,
k
.
Function Description
Complete the function countViralCombinations
in the editor below.
countViralCombinations
has the following parameter(s):
- string video: A string representing the lineup of TikTok segments, where each character corresponds to a segment's unique attribute or engagement level.
- int engagementArray[n]: An array of integers where each element is either '1' (indicating a viral segment) or '0' (indicating a non-viral segment). This array aligns with the English alphabet; for instance, 'a' corresponds to the first element.
- int k: An integer denoting the maximum number of non-viral segments allowed in any content formation to consider it viral.
Returns
int: an integer denoting the number of unique content formations that meet the criteria of having no more than k
non-viral segments, ensuring each formation is considered viral.
Constraints
- 1 ≤ n ≤ 1500.
- 1 ≤ k ≤ n.
- engagementArray[i] ∈ {0, 1}
Sample case 0:
video = "stream" engagementArray size = 26 engagementArray = [0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] k = 2 Output = 14
Another test case: Best for our understanding
Input:
video = "abc" engagementArray = [0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] k = 2
Output: 5
Explanation:
- "a" - 1 weak player - viral
- "b" - 0 weak players - viral
- "c" - 1 weak player - viral
- "ab" - 1 weak player - viral
- "bc" - 1 weak player - viral
- "abc" - 2 weak players - it is equal to k so - non-viral
This is a brute force solution, for subarrays:
def countViralCombinations(video, engagementArray, k): res = 0 n = len(video) for i in range(n): weak = 0 for j in range(n): if engagementArray[ord(video[j]) - ord('a')] != 1: weak += 1 if weak < k: res += 1 return res video = "abc" engagementArray = [0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] k = 2 print("result", countViralCombinations(video, engagementArray, k))
Please UPVOTE!!!!!!
any other solutions or approaches would be appreciated!
Candidate's Approach
No approach provided.
Interviewer's Feedback
No feedback provided.