Skip to main content

Posts

Showing posts from October, 2020

How to sort a string array by 2nd char of each string in the array ?

 Given a string array. Write a code to sort the strings in the array based on their 2nd character. Assumption :-  Each array element contains more than one character. Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class SortString { public static void main (String[] args) { String[] arr = { "heist" , "code" , "blog" , "python" , "java" }; for ( int i= 0 ; i<arr. length - 1 ; i++){ for ( int j=i+ 1 ; j<arr. length ; j++){ if (arr[i]. charAt ( 1 )>arr[j]. charAt ( 1 )){ String temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (String s:arr) System. out . println (s); } } Python  (using sort and lambda) arr = [ "heist" , "code" , "blog" , "python" ,

Contains duplicates

 Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1 Input: [1,2,3,1] Output: true Example 2 Input: [1,2,3,4] Output: false Java 1. Using Linear search (takes more time) 1 2 3 4 5 6 7 8 public boolean containsDuplicate ( int [] nums) { for ( int i = 0 ; i < nums. length ; ++i) { for ( int j = 0 ; j < i; ++j) { if (nums[j] == nums[i]) return true ; } } return false ; } 2. Using sorting 1 2 3 4 5 6 7 8 public boolean containsDuplicate ( int [] nums) { Arrays. sort (nums); for ( int i = 0 ; i < nums. length - 1 ; ++i) { if (nums[i] == nums[i + 1 ]) return true ; } return false ; } 3. Using HashSet 1 2 3 4 5 6 7 8 9 public boolean containsDuplicate ( int [] nums)

Find the running sum of an Array

nums = [ 3, 1, 2, 10, 1 ] runningSum = [ 3, 3+1, 3+1+2, 3+1+2+10, 3+1+2+10+1 ] runningSum = [ 3, 4, 6, 16, 17 ]  Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class RunningSum { public static void main (String args[]) { int [] nums = { 1 , 2 , 3 , 4 }; int [] runningSum = findRunningSum(nums); for ( int x: runningSum) System. out . print (x+ " " ); } public static int [] findRunningSum ( int [] nums) { int [] runningSum = new int [nums. length ]; int sum = 0 ; for ( int i= 0 ; i<nums. length ; i++){ sum += nums[i]; runningSum[i]=sum; } return runningSum; } } Python def runningSum(arr): result = [ None ] * len (arr) sum = 0 for i in range ( 0 , len (arr)): sum += arr[i] result[i] = sum return result arr = [ 1 , 2 , 3 , 4 ] print (ru

Check Array elements can make Arithmetic Progression

Example 1: Input: arr = [3,5,1] Output: true Explanation: We can reorder the elements as [1,3,5] or [5,3,1] with differences 2 and -2 respectively, between each consecutive elements. Example 2: Input: arr = [1,2,4] Output: false Explanation: There is no way to reorder the elements to obtain an arithmetic progression.  Constraints: 2 < arr.length <= 1000 -10^6 <= arr[i] <= 10^6 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import java.util.*; public class AP { public static void main (String args[]) { int [] arr = { 3 , 5 , 1 }; System. out . println (checkAP(arr)); } public static boolean checkAP ( int [] arr){ Arrays. sort (arr); // sort the array int diff = arr[ 1 ] - arr[ 0 ]; // difference for ( int i= 2 ; i<arr. length ; i++) if (arr[i]-arr[i- 1 ]!=diff) return false ; return true ; } } Pyt

Check If a Word Occurs As a Prefix of Any Word in a Sentence

 Given a sentence that consists of some words separated by a single space, and a searchWord. You have to check if searchWord is a prefix of any word in sentence. Return the index of the word in the sentence where searchWord is the prefix of this word. If search word is the prefix of more than word, return the index of first word. If there is no such word return -1. Ex:- Input:  sentence= "I love burger"  searchWord = "burg" Output:  2 Ex:- Input:  sentence= "I am tired"  searchWord = "you" Output:   -1 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class MyClass { public static void main (String args[]) { String sentence = "this problem is an easy problem" ; String searchWord = "pro" ; System. out . println (findIndex(sentence, searchWord)); } public static int findIndex (String sentence, String searchWord){ String[] arr = sentence. split (

Number of Students Doing Homework at a Given Time

Given two integer arrays startTime and endTime and given an integer queryTime . The ith student started doing their homework at the time startTime[ i ] and finished it at the time endTime[ i ] . Return the number of students doing their homework at time queryTime .   Example 1: Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4 Output: 1 Explanation: We have 3 students where: The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4. The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4. The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4. Example 2: Input: startTime = [4], endTime = [4], queryTime = 4 Output: 1 Explanation: The only student was doing their homework at the queryTime. Example 3: Input: startTime = [4], endTime = [4

Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Example 1: Input: [1,3,5,6], 5 Output: 2 Example 2: Input: [1,3,5,6], 2 Output: 1 Example 3: Input: [1,3,5,6], 7 Output: 4 Example 4: Input: [1,3,5,6], 0 Output: 0 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public class SearchInsertPos { public static void main (String args[]) { int [] arr = { 1 , 3 , 5 , 6 } ; int target = 0 ; System. out . println (searchInsPos(arr, target)); } public static int searchInsPos ( int [] arr, int target){ if (arr. length == 0 ) return - 1 ; int i = 0 ; while (i < arr. length && target > arr[i]){ i++

Merge two sorted array

 Write a function to merge two sorted arrays. Ex:- [ 1, 2, 4 ]  and [ 1, 3, 4 ] output:-  [ 1, 1, 2, 3, 4, 4] Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class MergeList { public static void main (String args[]) { int [] arr1 = { 1 , 2 , 4 }; int [] arr2 = { 1 , 3 , 4 }; int [] arr = mergeArray(arr1, arr2); for ( int x: arr) System. out . print (x+ " " ); // 1 1 2 3 4 4 } public static int [] mergeArray ( int [] arr1, int [] arr2){ int [] arr = new int [arr1. length + arr2. length ]; int i = 0 , j = 0 , k = 0 ; while (i<arr1. length && j<arr2. length ){ if (arr1[i] < arr2[j]) arr[k++] = arr1[i++]; else arr[k++] = arr2[j++]; } while (i < arr1. len

Print numbers from 1 to 10 without using loop

 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 public class PrintNumbers{ public static void main (String []args){ int n = 10 ; printWithoutLoop(n); } public static void printWithoutLoop ( int n){ if (n> 0 ){ printWithoutLoop(n- 1 ); System. out . println (n); } return ; } } Python def printWithoutLoop(n): if n > 0 : printWithoutLoop(n - 1 ) print (n) return n = 10 printWithoutLoop(n)

Longest Common Prefix

 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "" . Example:   input : [ "flower" , "flow" , "flight" ] output : fl input : [ "dog" , "racecar" , "car" ]  output : ""   (empty string) Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class LongestCommonPrefix{ public static void main (String []args){ String[] arr = { "flower" , "flow" , "flight" }; String result = findLongestCommonPrefix(arr); System. out . print (result); } public static String findLongestCommonPrefix (String[] arr){ String lcp = "" ; // longest common prefix int index = 0 ; if (arr== null || arr. length == 0 ) return lcp; for ( char c : arr[ 0 ]