an HCL GUVI product

String & Tries - DSA Interview Questions

String and Trie-based problems are essential in pattern matching, text processing, and search optimization. Strings are used in substring searching, palindrome checks, and compression algorithms, while Tries enable fast prefix lookups and dictionary-based searches. Understanding these concepts is crucial for solving problems efficiently.

Practice String & Tries DSA Coding Problems with Solutions

Learning Objectives:

Learn string manipulation techniques like reversal, searching, pattern matching, and compression. Understand Trie data structure for efficient word storage, prefix searches, and dictionary-based lookups.

Exercise Instructions:

  • Start with the first problem and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you with more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Reverse a String

Required Input:

s = 'hello'

Expected Output:

olleh

Code In Python

def reverse_string(s): # Write your logic here pass # Prefilled input s = "hello" print(reverse_string(s))

Run Code?

Click Run Button to view compiled output

2. Check Palindrome

Required Input:

s = 'madam'

Expected Output:

True

Code In Python

def is_palindrome(s): # Write your logic here pass # Prefilled input s = "madam" print(is_palindrome(s))

Run Code?

Click Run Button to view compiled output

3. First Unique Character

Required Input:

s = 'swiss'

Expected Output:

w

Code In Python

def first_unique_char(s): # Write your logic here pass # Prefilled input s = "swiss" print(first_unique_char(s))

Run Code?

Click Run Button to view compiled output

4. Count Vowels and Consonants

Required Input:

s = 'hello world'

Expected Output:

(3, 7)

Code In Python

def count_vowels_consonants(s): # Write your logic here pass # Prefilled input s = "hello world" print(count_vowels_consonants(s))

Run Code?

Click Run Button to view compiled output

5. Check Anagrams

Required Input:

s1 = 'listen', s2 = 'silent'

Expected Output:

True

Code In Python

def are_anagrams(s1, s2): # Write your logic here pass # Prefilled input s1 = "listen" s2 = "silent" print(are_anagrams(s1, s2))

Run Code?

Click Run Button to view compiled output

6. Longest Common Prefix

Required Input:

words = ['flower', 'flow', 'flight']

Expected Output:

fl

Code In Python

def longest_common_prefix(words): # Write your logic here pass # Prefilled input words = ["flower", "flow", "flight"] print(longest_common_prefix(words))

Run Code?

Click Run Button to view compiled output

7. Most Frequent Character

Required Input:

s = 'character'

Expected Output:

c

Code In Python

def most_frequent_char(s): # Write your logic here pass # Prefilled input s = "character" print(most_frequent_char(s))

Run Code?

Click Run Button to view compiled output

8. Check Digits Only

Required Input:

s = '123456'

Expected Output:

True

Code In Python

def is_numeric(s): # Write your logic here pass # Prefilled input s = "123456" print(is_numeric(s))

Run Code?

Click Run Button to view compiled output

9. Remove Consecutive Duplicates

Required Input:

s = 'aaabbcddd'

Expected Output:

abcd

Code In Python

def remove_consecutive_duplicates(s): # Write your logic here pass # Prefilled input s = "aaabbcddd" print(remove_consecutive_duplicates(s))

Run Code?

Click Run Button to view compiled output

10. Longest Unique Substring

Required Input:

s = 'abcabcbb'

Expected Output:

3

Code In Python

def longest_unique_substring(s): # Write your logic here pass # Prefilled input s = "abcabcbb" print(longest_unique_substring(s))

Run Code?

Click Run Button to view compiled output

1 of 3