Skip to main content

Posts

Showing posts with the label Data Structure

Pangram : Problem on hackerrank

This is a problem mentioned on Hackerrank. Below is my attempt to solve this problem. Problem Statement Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence "The quick brown fox jumps over the lazy dog" repeatedly because it is a pangram. ( pangrams are sentences constructed by using every letter of the alphabet at least once. ) After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s , tell Roy if it is a pangram or not. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution {     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         char[] str = in.nextLine().toCharArray();         boolean[] resultArr = new boolean[26]; int smallMax = 'z'; int...

Binary Search

You have a big list of numbers and you want to search an element from it, how to do it? Simplest approach would be to compare this number with all the elements from the list. In this case, if the element is the last element then we have to go through all the elements till we reach the element we want and what if it doesn't exist. There are many searching algorithms to get it done efficiently, one of them is Binary Search. In binary search : Step 1. Sort the list of elements in ascending order Step 2. Check the value of element that needs to be search (lets call this 'x') with the middle element (midElement) in the list. Step 3. If the value of x is greater than midElement then the value is present in the right half of the array or else it is present in the left half of the array. Step 4. Depending on the right or left half of the array, x will be compared. Lets assume it is present in left half, then the leftMidElement is compared with x and then Step 3 is repeated until w...

Hashing

Consider a use case of Electronic health record where you have a unique key for every record and you have some data associated with it like persons name, address etc. then how can you handle this data. Some ways to store this information would be to use array, linked list, trees or may be direct access table. But all these also have some problems as below : In case of array and linked list if you want to search for a record you have to search linearly. We can keeps them sorted so we can apply some searching algorithm and can get faster search results but this will create another problem of inserting  and deleting. In this case every time you insert/delete a record you must also check for the order of the array/linked list. If we consider a balanced tree then we can get the results for search/insert/delete in moderate amount of time. O(Logn) time. Out of all these, Direct Access Table shows some promising results. Direct Access Table is nothing but a huge array where the inde...

Difference Between LinkedList and Arraylist

Array list and linked list both implements list interface. ArrayList is more popular amongst programmer.The main difference between Arraylist and Linked list is arraylist implemented using re sizable array while linkedlist is implemented using doubly linked list. ArrayList LinkedList Arraylist is an index based data structure.Array provides O(1) performance for get(index) method but remove is costly in ArrayList as you need to rearrange all elements. LinkedList doesn't provide Random or index based access and you need to iterate over linked list to retrieve any element which is of order O(n). In case of arraylist for updating the list anywhere other than end of the list need re-indexing of the list. Insertion are easy and fast in linked list because there is no risk of resizing the data and copying the data into new array. ArrayList only have the data object Linked list has both data and address to the next link.Hence linked list has more ...