1. Design an algorithm that rearranges the string characters from end to start.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "hello" | "abcdefghijklmnopqrstuvwxyz" | "" (empty string) |
Output | "olleh" | "zyxwvutsrqponmlkjihgfedcba" | "" |
Explanation | A normal word is reversed successfully. | A long string with maximum distinct characters is reversed. | An empty string remains the same after reversal. |
2. Design an algorithm that returns '1' if a given string is a palindrome else returns '0'.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "racecar" | "hello" | "" (empty string) |
Output | 1 | 0 | 1 |
Explanation | The input is already a palindrome, so the function quickly returns 1. | The input is not a palindrome, requiring full comparison before returning 0. | An empty string is considered a palindrome, so the function should return 1. |
3. Design an algorithm that returns the position (0- based index) of the first character that has no duplicates in the given string.
if no such character exist in the string the algorithm should return '-1'.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "abcdef" | "aabbccddeeff" | "aaaaaa" |
Output | 0 | -1 | -1 |
Explanation | The input is already a palindrome, so the function quickly returns 1. The first character 'a' is unique and appears only once, so the function returns 0 immediately. | Every character appears more than once, so there is no non-repeating character, returning -1. | All characters are identical, meaning no unique character exists, so return -1. |
4. Design an algorithm that returns '1' if the given two strings 'A' and 'B' are anagrams else returns '0'.
Anagram - Two strings are anagrams if they contain the same characters with the same frequency and have the same length.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | Parameter: "listen", "silent" | "abcdefghij", "jihgfedcba" | "a", "b" |
Output | Output: 1 | 1 | 0 |
Explanation | Both strings have the same characters with the same frequency and are of the same length. | Both strings contain the same characters with the same frequency but are in reverse order, making them valid anagrams. | The strings have different characters, so they cannot be anagrams. |
5. Design an algorithm that returns the longest substring that contains no repeating characters in string 's'
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "abcdef" | "aaaaaa" | "" |
Output | "abcdef" | "a" | "" |
Explanation | The entire string "abcdef" has unique characters, so the longest substring is itself. | All characters are the same, so the longest substring with unique characters is just "a". | The input is an empty string, so there is no valid substring, returning "". |
6. Design an algorithm that returns a 'count and say' string by processing the given string 's' by following these rules:
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "1111" | "121212" | "" (Empty String) |
Output | "41" | "111211121112" | "" |
Explanation | The entire string consists of one repeating digit ('1'), so we count 4 occurrences and return "41". | Every digit alternates, meaning each group contains only one digit, leading to a long output string where each digit is counted as 1. | If the input string is empty, there are no digits to process, so the output remains empty. |
7. Design an algorithm that returns the nth 'count and say' term.
The 'count and say' sequence is a series of strings where each term is generated by reading the previous term and describing its groups of consecutive digits.
The first term is '1', the second term is '11' (one 1), the third term is '21' (two 1s), the fourth term is '1211' (one 2, one 1), and so on.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | n = 1 | n = 10 | n = 0 |
Output | "1" | "13211311123113112211" | "" |
Explanation | The first term of the sequence is always "1" and requires no further processing. | The 10th term is generated by iteratively applying the "count and say" transformation on the previous term. The sequence grows exponentially in length. | If n = 0, there is no valid term in the sequence, so an empty string is returned (or could raise an error based on implementation). |
8. Design an algorithm that returns the starting index (0-based index) of a given 'substring' present in the given 'main string'.
If the substring is not present in the mainstring, the algo should return -1.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | "helloworld", "hello" | "aaaaaaaab", "aab" | "abcdef", "xyz" |
Output | 0 | 6 | -1 |
Explanation | The substring "hello" appears at the very beginning of the main string, making it an optimal scenario. | The substring "aab" is found only at the second last position in "aaaaaaaab", requiring maximum comparisons before finding a match. | The substring "xyz" is not present in "abcdef", so the function returns -1. |
1. Design an algorithm that scans a string 's' and returns the longest consecutive substring that form a palindrome.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
input | "abacdfgdcaba" | "abcdefg" | "a" |
output | "aba" (or "aca") | "a" (or any single character) | "a" |
explanation | The input string contains multiple palindromic substrings ("aba", "aca"), and the longest one is returned. This case runs efficiently. | The input string has no repeated characters, so the longest palindrome is just a single character. The algorithm must scan the entire string, making it the worst case in terms of performance. | The smallest input size (one character) is itself a palindrome, ensuring the algorithm correctly handles minimal input cases. |
2. Design an algorithm that takes in array of strings and returns a list of group arrays where each group contains words that are anagram.
Anagram - Two strings are anagrams if they contain the same characters with the same frequency and have the same length.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
input | ["bat", "tab", "cat", "act"] | ["abcdef", "fedcba", "ghijkl"] | ["aaa", "aaa", "aaa"] |
output | [["bat", "tab"], ["cat", "act"]] | [["abcdef", "fedcba"], ["ghijkl"]] | [["aaa", "aaa", "aaa"]] |
explanation | Efficient sorting & grouping | Computationally expensive | Fastest case |
3. Given a main string "S" and a target string "T". Design an algorithm that finds the smallest contiguous substring in main string that contains all characters of T (including duplicates), in any order.
If no such substring exists, returns an empty string.
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
input | S = "abcdefghijk", T = "abc" | S = "aa...aaabc", T = "abc" (Long S, T at the end) | S = "abcdef", T = "xyz" (No match) |
output | "abc" | "abc" | "" (empty string) |
explanation | The required substring is found at the beginning of S, requiring minimal iterations. | The entire string must be scanned before finding the required substring at the end of S, leading to maximum iterations. | No characters from T are in S, so no valid substring exists, returning an empty string. |
4. Given a string containing only three types of brackets:
Design an algorithm that determine if the input string is valid, if valid returns 1 else 0.
A valid string satisfies the following conditions:
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
input | "()" | "{[({[({[()]})]})]}" | "])" |
output | 1 | 1 | 0 |
explanation | The input has the smallest valid structure with matching parentheses. | A deeply nested valid input with all opening and closing brackets properly ordered. | The input starts with a closing bracket before an opening bracket, which is invalid. |
5. Design an algorithm that takes in a string 's' and compresses consecutive duplicate characters into a single instance of the character followed by its count and returns the compressed string.
If a character appears only once, it should remain unchanged e.g., "abc" - "abc" not "a1b1c1"
parameters \ cases | Best Case | Worst Case | Edge Case |
---|---|---|---|
input | "abcdef" | "aaaaaaaabbbbbbbcccccccc" | "" (empty string) |
output | "abcdef" | "a8b7c8" | "" |
explanation | Since there are no consecutive duplicate characters, the string remains unchanged. | The input has long consecutive repeating characters, resulting in maximum compression. | An empty string should return an empty string without any modifications. |
6. Given a string 'S' and an integer 'k', Design an algorithm that returns the k th permutation of 'S' in lexicographical order.
If k is larger than the total number of unique permutations, return an empty string.
Parameter \ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Parameters | S = "abc", k = 1 | S = "aaaaaaa", k = 1 | S = "abc", k = 7 |
Output | "abc" | "aaaaaaa" | "" (empty string) |
Explanation | - Small input. - First permutation directly. - No backtracking needed. | - All characters are same. - Only 1 unique permutation exists. - Any k will result in same output. | - k is larger than total unique permutations (which is 6 for "abc"). - Returns empty string. |
7. Given a string containing words seperated by spaces. Design an algorithm that reverses the order of words.
Parameter \ Case | Best Case | Worst Case | Edge Case |
---|---|---|---|
Input String | "hello" | "a quick brown fox jumps over the lazy dog" | "" (empty string) or string with multiple spaces " " |
Output | "hello" | "dog lazy the over jumps fox brown quick a" | "" (empty string) |
Explanation | Single word → no reversal needed | Maximum number of words → requires full in-place reversal | No words → should return empty string cleanly |