Join us

Tackling Array and String Interview Questions: Methods and Code

1_5ri8W3LSfnLHSFVd-ghUbw.jpeg

I Will walk you through the array and strings interview questions that may be asked and how to approach these problems, I will do 3 and leave the rest as a challenge.

Data Structures and algorithms are what sets apart a good and average developer. This has prompted multiple companies to set their coding interviews around data structures and algorithms.

I Will walk you through the array and strings interview questions that may be asked and how to approach these problems, I will do 3 and leave the rest as a challenge.

Problem 1: is unique

We are asked to implement an algorithm that checks if a string has unique characters, meaning there are no repeating characters.

Before we dig into the solution, we need to determine if we are working with an ASCII or Unicode string?

If we are working with ASCII then we only need to deal with only 128 unique characters and if we are working with Unicode we have to deal with 280 unique characters.

Let's assume we are dealing with an extended ASCII string, that can accommodate 256 characters.

We will be using the string “The Maths Geek” inside our array/list with an of 256, as we are working with extended ASCII.

To check each character in the string we need to make use of a Boolean array/list and set our default value to False.

After declaring our array/list we need to get the characters from the string and get a specific character. To achieve this we will be using the chars method and store it in an integer variable to get the ASCII value of that character.

After finding the ASCII value we will update the lists index location to True, the process is repeated in our loop with H,E,M,A,T,H,S,G,E,E,K .

Now if we have repeating characters we still store the ASCII value, check the index value again and if we find a repeating index location as it is already True we return False.

Moving on to the code implementation;

Problem 2: Check Permutation

With problem 2 we are given two different strings “god” and “dog” and asked to check if they are permutations of each other.

We first need to clarify the following with the interviewer;

Are the case of the characters sensitive such as “God” and “dog” if they are then they are not permutations of each other.

The second question we need to ask is are white spaces applicable, if a string is “dog ”, then if there is space within the last character which is g and our quotation marks, then it cannot be considered a permutation.

We will assume that there are no whitespaces and that our strings are case sensitive.

First we will seek to order both strings as they are in different order, we will do that using the sort() and .join() method.

Then we will compare both strings, if they are equal to each other, we assume they are permutations of each other.

Code Implementation

Problem 3: URLify

We are given what our function should do and the True length of our array/list which is 13, we want to store Mr John Smith within it.

Code Implementation

The code implementation is quite explanatory.

Questions to Complete on your own


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies, reach more readers and earn rewards!

Join other developers and claim your FAUN account now!

Avatar

The Maths Geek 🤓

@thenjikubheka
Mathematician | Software Engineer | Amazonian| Open Source | Blogger | Ban Killer Robots
User Popularity
204

Influence

20k

Total Hits

22

Posts

Mentioned tools