Posts

Showing posts from May, 2020

Reverse Words in a Sentence Without using any Library

Given a sentence or a string, our task is to reverse an order of words and print them. For example: Input: "python is love" Output: "love is python" The idea is to solve this problem is using split() and join() methods. First, split the sentence into words than change the order of words, then join then as one sentence. Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Find the length of the longest substring without repeating character

Given a string, our task is to find the length of the longest substring without repeating any character. For example, the longest substring for "ABACDBFB" this string is "ACDBF" length of this string is 5. This problem can be solved with O(N) time complexity where N is the length of the string. Let's implement that: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Check if Two Strings are Rotation of Each Other

Given two strings, "string1" and "string2" our task is to check that string2 is a rotation of string1. For example: Input: string1 =  "awesomepython" string2 = "pythonawesome" Output: "Yes" The basic method of solving this problem is to concatenate one string with itself then check if another string is a substring of a concatenated string. Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, check out this article " Top 10 String Interview Questions ". All the Best!

Find all Permutations of a String

Given a string, our task is to find all permutations of a string. A Permutation is also called an arrangement of things.  A string of length N has N! (factorial) permutations. Want to learn more about permutation follow this link " Permutations ". For example: Input: "PRO" Output: ['PRO', 'POR', 'RPO', 'ROP', 'OPR', 'ORP'] This native method to solve this problem using backtracking. This problem can be solved easily using the itertools module. Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the best!

Count Number of Vowels and Number of Consonants from a String

Given a string, our task is to determine how many vowels and consonants it has. For example: Input: string = "pythonisawesome" Output: vowels: 6 consonants: 9 This problem can be solved by maintaining two variables: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Count The Occurrence of a given Character in String

Given a string and a character that we have to search. This problem can be used to solve many problems like  removing duplicates or removing unwanted characters from strings. This problem can be solved using count() or using a collections module or using the native method. For example: Input: string = "pythonisawesome",  char = "e" Output: 2 Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Check if a String is a Palindrome

In this article, you'll learn how to check a string is palindrome or not. A Palindrome is a string that can be read from both sides. For example:  Input: string = "mom" Output: True Input: string = "brother" Output: False If you know how to reverse a string or an array then this might be a very easy problem for you. If you don't know, please check out this article " Reverse an Array in Python ". Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Check if Two Strings are Anagram of Each Other

Given two strings, our task is to check whether these strings are an anagram of each other or not. An Anagram of a string is a string that contains the same characters, the order of characters can be different. For example: Input: s1 = "hackyourcode" s2 = "codehackyour" Output: True Input: s1 = "pythoniseasy" s2 = "pythonisawesome" Output: False The most common method to solve this problem is to sort strings, then compare each character but this method takes O(NlogN) time to perform. This problem can be solved efficiently O(N) using HashTable . Think about it! Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Find Duplicate Characters in a String

Given a string with repeated characters, our task is to print characters which are occurring more than one time in a string. For example:  Input: s = "pythonissoeasy" output: "s" "o" "y" To find duplicates in a string, we need to count the occurrence of each character in a string. If count is greater than 1 then simply print that character. The Native Method to solve this problem is using two nested for loops which take O(N^2) time to perform. But to solve this problem efficiently we can use Hashmap or Dictionary. Think about it! Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Remove All Blank Spaces from a String

Given a string with tons of whitespaces, our task is to remove them. For example:  Input: s = "   h a c k y o  u r c o de  "  output: "hackyourcode" There are a lot of ways to remove whitespace from a string.  Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Top 10 String Interview Questions

Image
Top 10 String Interview Questions with Solutions Along with array and other data structures, Strings is another popular topic. The good thing about strings is that if you know how to solve array-based questions you can easily solve a string-based question because the  string is nothing but a series of a character array. From the above figure, it is easy to understand strings. Here are some of the most frequently asked string-based questions: Remove all blank spaces from a string? ( Solution ) Print duplicate characters from a string? ( Solution ) Check if two strings are an anagram of each other? ( Solution ) Check if a string is a palindrome? ( Solution ) Count the occurrence of a given character in a string? ( Solution ) Find all permutation of a string? ( Solution ) Check if two strings are rotation of each other? ( Solution ) Count a number of vowels and the number of consona...

Finding the Peak Element from an Array

Finding The Peak Problem The peak element is an element that is greater than it's neighbors. In this problem, we have given an integer array and our task is to find the peak element from it. If the array contains more than one peak it will return only one. For example: arr = [3,5,4,8] here 5 is the peak element (5 is greater than 3 and 4). This problem can be solved using Binary Search Algorithm in O(logN) time where N is a length of an array or the number of elements in the array. Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Search an element in Rotated Sorted Array

Search an Element Given a rotated sorted integer array, our task is to find out the "target" element.  This can be solved using Binary Search Algorithm in O(logN) time. Let's see this in action: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Find Maximum and Minimum Number from Unsorted Array

Maximum and Minimum Number from an Array Given an integer array, our task is to Find Maximum and Minimum number from that in just one go! Let's do it: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Check for Valid Mountain in Array

Image
Check if Array form Valid Mountain or not Given an integer array, your task is to check if elements in array form Valid Mountain or not. If yes then return True else False. That means elements should be in strictly increasing order up to some instances after that in strictly non-decreasing order. Hopefully, the following figure clear your idea! Now let's see this in action: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Find all Pairs Whose Sum is Equal to Given Number

Find All Pairs form Array Whose Sum is Equal to Given Number Given an integer array and a number "sum" , Print all the pairs of integers whose sum is equal to "sum" . For example: arr = [5,3,2,8,4,7] and sum = 7 So, thw output will be (5,2), (3,4) This problem can be solved using the following method: 1. Native Method that uses nested for loops. 2. Using Hashmap or Dictionary Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Find Missing Numbers from Integer Array

Missing Numbers from Integer Array You are given a list n integers and these integers are in a range from 1 to n. There is one more than one missing number that you have to find. For example: arr = [1,2,3,5,6,8], the output will be [4,7] This can be solved using various methods: 1. Method using HashSet 2. Method One liner python code To find only one missing number from an integer array , the following methods can help you: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Sort an Array in Place using Quicksort in Python

Sort Array using Quicksort Algorithm Quicksort is an efficient sorting algorithm whose worst-case performance is O(n^2). Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. This can be done in-place, requiring small additional amounts of memory to perform the sorting. Want to know more about Quicksort  check out this link . Now, Let's implement it: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems check out this article " Top 10 Array Interview Questions ". All the Best!

Find Duplicate Numbers from an Integer Array

Duplicate Numbers in Integer Array In this article, I'll share how you can find out all the duplicate numbers from an integer array. Without wasting any time let's dive into the solution: You can easily solve this problem using two nested for loops but it will take O(n^2) time to perform. To solve this problem efficiently you can use extra space. Here is the solution: Now let's look at some common methods: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems please check out this article " Top 10 Array Interview Questions ". All the Best!

Remove Duplicate from Array in Place in Python

Image
Remove Duplicate from Array in Place In this article, I'll share some methods to remove duplicates from an array one of them is how to do it in place that means with O(1) space complexity. Now, we can do this by maintaining an extra pointer "x" which holds the last index of a nonduplicate value in an array. Let's make it simple, of course, the length of a resultant array will be less than the original array that contains duplicate values. So by shifting all nonduplicate values to the front of an array might solve our problem. Look at the following figure: But to perform the in-place operation you need to sort the array first! which take O(nlogn). Here are some native methods: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comme...

Reverse an Array in Place in Python

Reverse an Array in Place In this article, I'll share the solution to reverse an array in place along with some traditional methods that can be used to reverse an array in python. Now, this problem can be solved using two pointer approach, Think about it. Take a two-pointers to suppose "i" and "j" where "i" holds the starting index of an array and "j" holds the last index of an array then starts swapping two elements at index "i"   and "j" until you reach the middle. Let's see this in action: There are some other methods to reverse an array but instead of reversing an array at its own place, they'll need some extra space O(n) to store reversed array. Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more prob...

Top 10 Array Interview Question with Solution

The array is one of the most frequently asked data structures during the interview. It is one of the favorite topics of the interviewer. The array is a data structure that stores an element in contiguous memory locations. To solve an array-based question of course you should have good knowledge of the array data structure as well as some basic programming knowledge of loops, recursion, and fundamental operations (like arithmetic). In this article, I'll share some frequently asked interview questions on the array data structure as well as their solutions in a python programming language . Here is the list of Top 10 Array-based questions: Reverse an array in place? ( Solution ) Remove duplicate from an array in place? ( Solution ) Find duplicate numbers from integer array if it contains duplicates? ( Solution ) Sort an integer array in place using the Quicksort algorithm? ( Solution...

Prepare Yourself for a Coding Interview with Data Structures in Python

Image
Learn Basic Data Structures Practical Approach - 1 In Computer Science, graduates and programmers are applying for programming and software development roles at startups and big organizations like Google, Amazon, Microsoft many more. As a graduate, it's expected from you to have strong knowledge of both basic data structures like Array, Linked List, Stack, Queue, Binary Tree, Hash Table, and advanced data structures like Binary Heap, Buffer, etc. It does not matter whether your a java developer or a web developer. In this article, I will share the basic concepts of frequently asked Data Structures and Algorithms in the interviews along with some questions based on that data structure. (Solutions will be available only in python3) Prerequisites:- At least familiar with coding. Let's start with What is Data Structure? ...