Basic Tech Practice

Ace your homework & exams now with Quizwiz!

How do you remove all occurrences of a given character from the input string?

Use the built-in string method "replace"

How to find out if the given two strings are anagrams or not?

Check the length. Use build in sort function and compare

Find the number of occurrences of a character in a String?

O(n) — Linear time complexity

How do you calculate the number of vowels and consonants in a String?

O(n) — Linear time complexity

How do you check if the given number is prime?

O(n) — Linear time complexity

How do you sum all the elements in an array?

O(n) — Linear time complexity

How would you find the second largest number in an array?

O(n) — Linear time complexity

How would you implement the bubble sort algorithm?

O(n) — Linear time complexity

Reverse a string?

O(n) — Linear time complexity

How do you determine if a string is a palindrome?

O(n) — Linear time complexity If reversed str is equals to initial str Or divide str into 2 half, 1 of which is reversed version and compare

How would you implement the insertion sort algorithm?

O(n**2) for i in range(1, len(l), 1): j = i while j > 0 and l[j-1] > l[j]: temp = l[j] l[j] = l[j-1] l[j-1] = temp j -= 1

How would you implement the bubble sort algorithm?

O(n**2) for i in range(len(l)): for j in range(len(l)-i-1): if l[j]>l[j+1]: temp = l[j] l[j] = l[j+1] l[j+1] = temp

Reverse an array?

O(n/2)

Explain overloading and overriding?

Overloading: When a class has two or more methods with the same name, they are called overloaded methods. Overriding: When a superclass method is also implemented in the child class, it's a case of overriding.

How would you implement Binary Search?

def binary_search( arr:list=[1, 2, 3, 4, 4, 5], low:int=0, high:int=6, x:int=4 ) -> int: if low <= high: # Divide array by 2 mid = (low + high) // 2 if x == mid: return mid elif arr[mid] > x: return binary_search(arr, low, mid - 1, x) else: return binary_search(arr, mid + 1, high, x) else: raise Exception(f'Low value {low} is higher the {high}')

How do you find the factorial of an integer?

def factorial(l:int=5) -> int: if l == 1: return 1 return l * factorial(l-1)

Print a Fibonacci series using recursion?

def fibonacci(n:int=9) -> int: if n <= 1: return n return fibonacci(n-1) + fibonacci(n-2)


Related study sets

Help Desk Final (Chapter 1 - 11) Multiple Choice

View Set

California Real Estate Chapter 7

View Set

US History Exam Semester 1 (final)

View Set

GS ENVS 302 CH 12 Climate Change

View Set