Last active
September 26, 2016 21:50
-
-
Save amadrzyk/345e76b03bd0cdf1f69b13c98713ecf9 to your computer and use it in GitHub Desktop.
Week 1 – Western Tech Interview Prep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashSet; | |
| import java.util.Iterator; | |
| public class Intersection { | |
| public static int[] intersection(int[] nums1, int[] nums2) { | |
| /* | |
| ALGORITHM STEPS | |
| put everything from second array into hashset | |
| go through every item in second array and check if it's in hashset | |
| if it is, put it into a new hashset | |
| convert that hashset to array | |
| */ | |
| HashSet<Integer> setTwo = new HashSet<Integer>(); | |
| for (int i = 0, n = nums2.length; i < n; i++){ | |
| setTwo.add(nums2[i]); | |
| } | |
| HashSet<Integer> setOne = new HashSet<Integer>(); | |
| for (int i = 0, n = nums1.length; i < n; i++){ | |
| if (setTwo.contains(nums1[i])){ | |
| setOne.add(nums1[i]); | |
| } | |
| } | |
| int[] array = new int[setOne.size()]; | |
| Iterator<Integer> iterator = setOne.iterator(); | |
| for (int i = 0, n = setOne.size(); i < n; i++){ | |
| array[i] = iterator.next(); | |
| } | |
| return array; | |
| } | |
| public static void main(String args[]){ | |
| int[] nums1 = {1,2,3,4,5}; | |
| int[] nums2 = {2,3,4,5,5,5,5,6}; | |
| int[] nums3 = intersection(nums1, nums2); | |
| for (int num : nums3){ | |
| System.out.println(num); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MergeArrays { | |
| public static void merge(int[] nums1, int m, int[] nums2, int n) { | |
| /* | |
| STEPS TO IMPLEMENT | |
| create last possible index for nums1 and nums2 | |
| compare nums1[index1] vs nums2[index2], place the larger one at the | |
| (combined) last possible index of nums1 | |
| decrement that index | |
| */ | |
| int index1 = m-1; | |
| int index2 = n-1; | |
| for (int i = m + n - 1; i >= 0; i--){ | |
| // IF FINISHED WITH NUMS1 | |
| if (index1 < 0){ | |
| nums1[i] = nums2[index2]; | |
| index2--; | |
| } | |
| // IF FINISHED WITH NUMS2, BREAK (NUMS1 ALREADY SORTED) | |
| else if (index2 < 0){ | |
| break; | |
| } | |
| else if (nums1[index1] > nums2[index2]){ | |
| nums1[i] = nums1[index1]; | |
| index1--; | |
| } | |
| else { | |
| nums1[i] = nums2[index2]; | |
| index2--; | |
| } | |
| } | |
| } | |
| public static void main(String args[]){ | |
| int[] nums1 = {17,17,88,99,100,0,0,0,0,0}; | |
| int m = 5; | |
| int[] nums2 = {2,4,99,888}; | |
| int n = 4; | |
| merge(nums1, m, nums2, n); | |
| for(int i = 0; i < m + n; i++){ | |
| System.out.println(nums1[i]); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Palindrome { | |
| public static boolean isPalindrome(String s) { | |
| /* | |
| STEPS TO IMPLEMENT | |
| remove any non-alphanumeric characters | |
| convert all to uppercase() | |
| create new string with reverse | |
| see if both are equivalent with .equals() | |
| */ | |
| int length = s.length(); | |
| String stripped = ""; | |
| for (int i = 0; i < length; i++){ | |
| char letter = s.charAt(i); | |
| // CHECK ALNUM | |
| if (Character.isLetterOrDigit(letter)){ | |
| // CONVERT TO UPPER | |
| if (Character.isLowerCase(letter)){ | |
| letter = Character.toUpperCase(letter); | |
| } | |
| // CONCATENATE WITH STRIPPED STRING | |
| String sLetter = Character.toString(letter); | |
| stripped += sLetter; | |
| } | |
| } | |
| // Reverse the stripped string | |
| int lenStripped = stripped.length(); | |
| String newStr = ""; | |
| for (int i = lenStripped - 1; i >= 0; i--){ | |
| char letter = stripped.charAt(i); | |
| String sLetter = Character.toString(letter); | |
| newStr += sLetter; | |
| } | |
| // Check if equivalent | |
| return stripped.equals(newStr); | |
| } | |
| public static void main(String[] args){ | |
| String string1 = "race car"; | |
| String string2 = "race2 car"; | |
| System.out.println(isPalindrome(string1)); | |
| System.out.println(isPalindrome(string2)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment