Easy Algorithms Flashcards

1
Q

Remove Duplicates from Sorted Array

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

A

Using two pointers

def removeDuplicates(nums):
if not nums:
return 0
slow = 0
for fast in range(1, len(nums)):
if nums[fast] != nums[slow]:
slow += 1
nums[slow] = nums[fast]
return slow + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

An engineer wants to improve his word choice. Notify engineer when the prohibited word is used.

A

def improve_word_choice(text):
# Define the prohibited words
prohibited_words = {‘basically’, ‘like’}

# Remove punctuation and convert text to lowercase
cleaned_text = ''.join(char.lower() if char.isalnum() or char.isspace() else ' ' for char in text)

# Split the cleaned text into words
words = cleaned_text.split()

# Count occurrences of prohibited words
word_counts = {word: words.count(word) for word in prohibited_words}

# Construct the output message
if word_counts:
    message = "Prohibited word(s) found! You have used:\n"
    for word, count in word_counts.items():
        message += f"{word}: {count} time(s)\n"
else:
    message = "Congratulations! You haven't used any prohibited words!"

return message

Test the function
print(improve_word_choice(“The two approaches are basically very similar.”))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Find Duplicates in Array

  1. lets say there is an array , find if it has duplicates ​examle : x = [2,3,4,5,6,7,3]​ ANs: 3​
A

def has_duplicates(nums):
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False

Example usage:
x = [2, 3, 4, 5, 6, 7, 3]
print(has_duplicates(x)) # Output: True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Find the second smallest number in the array

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Given two strings s and t, determine if they are isomorphic.

Two strings s and t are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of
# characters. No two characters may map to the same character, but a character may map to itself.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Having the following string “Globant is a great company, Globant has great globers”
#Find the amount of times that every single word is showing up in the string

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Having the following string “fds76f8ds6f7 67fds6f7 67fsd67”
#get the sumatory of the numbers inside of the string

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Having the following list: [1,2,3,4,5,6]
# Swap the first and last value

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Plus one

You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0’s.

Increment the large integer by one and return the resulting array of digits.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Two sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many time letter j appears in the sting

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly