Coding Questions

Ace your homework & exams now with Quizwiz!

Median of Two Sorted Arrays: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

First we want to create two ints initialized with the length of array one and array two. Also create a last index int that is set to -1, this decreases your index. Next we want to create a new array that is the size of arrays one and two, initialize it to 0 as well will merge in the numbers next. Now, while i < array 1 and j < array 2, if value i is less than or equal to value j, then set mergeNums to the nums1[i] index. Last index starts at -1 and is set to ++lastIndex. This makes is so you add index 0 first and then adds one every time. If i is > j then merge j instead. In situations were there is an emptu array, we have while loops with the same process but they only loop through list n1 or n2. After this, add the lists and then return n%2?mergeNums[n/2]:(mergeNums[n/2-1]+mergeNums[n/2])/2.0; This finds median in c++.

Longest Palindromic Substring: Given a string s, return the longest palindromic substring in s.

First, a solve function can be used to return a boolean value indicating whether the substring s[i...j] is a palindrome or not. If i == j then it is only one value which is always true. Else if i and j are true, and if i+1 and j-1 is already marked as true, then return true. The function longestPalindrome takes a string s as input and returns the longest palindrome substring of s. The variable n is initialized with the length of the string s. The 2D boolean vector dp is initialized with false for all entries. dp[i][j] is set to true if the substring s[i...j] is a palindrome, and false otherwise. The two nested loops iterate over all possible substrings of the string s and fill in the dp table for all the substrings. The outer loop g represents the length of the substring, and the inner loop represents the starting and ending index of the substring. For each substring, the solve function is called to check if it is a palindrome or not. If it is a palindrome, the startIndex and maxLength variables are updated to store the starting index and length of the current longest palindrome substring found so far. Finally, the function returns the substring of s starting from the startIndex and of length maxLength. The time complexity of this algorithm is O(n^2), since it iterates over all possible substrings of the input string s. The space complexity is also O(n^2) to store the 2D boolean vector dp.

Zigzag Conversion: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows);

Our smallest case would be 1 char or less, in which case we return the string. Next, declare a vector that is a list of the number of rows as well as blank space. Create an int for the current line being visited, this is 0. Create bool saying if you reached the edge of the line. Now, for the length of the entered string, starting at index 0 and adding one per loop, check to see if current line equals 0, or if it equals the number of rows minus one. If yes, set the bool to false. Check the vector at index current line, add to it index i of the string. If you have not reached the edge add +1 to current line, else, subtract one. Create a string for the result. For the length of the number of rows, add vector index i to the result and return it when done.

Adding Two Numbers: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

To start create two new lists- solution and the temporary solution. Also create an int for carrying over numbers when necessary. While the two lists are not null, or carry is true (1), set a int sum to 0. If l1 is nto mill then add the next value to the sum and point to the next value of l1. If l2 is not null then do the same for sum and l2 as was done in l1. Add carry to sum, and set carry to sum / 10. This is a floor so it will round down. Create a new node that is sum % 10 and set it to the next node in the temporary list, set temporary list to next node which is null. At the end return the solution which points to the temporary list we populated through the loop.

Two Sum: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Using a map we can create key value pairs for the list array nums. For the length of nums, create an int n set to the nums index on each loop. Search for the number needed to equal target by subtracting target from n. If this number is found in the map, return the indices of the numbers. Keep in mind with maps we use a key to find a pair so returning the key gets the value indice(?) O(n)- linear in the size of the input array

Longest Substring Without Repeating Characters: Given a string s, find the length of the longest substring without repeating characters.

We want to create two ints for length, and max length. We also need a mp map with a char key and int value. For loop through the size of the string, adding each string index to the map. Using mp[s[i]]++ adds {char: frequency} so we can track how many times the char appears. While a char has more than 1 appearance, remove one instance of the value and subtract 1 from length. Out of this loop, set max length to the max of maxlength and length, then return max length.


Related study sets

pharmacology - chapter 2- prep u

View Set

ACSR 8 - Commercial Auto Loss Exposures and the Business Auto Coverage Form

View Set

C214 WGU Financial Management End of Section Questions

View Set

Biochemistry of Foods: Vitamins Review Questions

View Set

Silvestri Comprehensive Review for the NCLEX-PN® Exam, 7th Edition - Pain Flashcard Set

View Set

리딩파워 유형편[완성] 10과 2번 해석

View Set

Speech Quiz #2 Lecture Quiz Questions

View Set