Data structure interview questions (Swift) Part-II

Covered Questions: 1. Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new…

Data structure interview questions (Swift) Part-II

Covered Questions:
1. Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
2.Design an algorithm to find the maximum profit .
3. Given an array, rotate the array to the right by k steps, where k is non-negative.Could you do it in-place with O(1) extra space?
4. Given an array of integers, find if the array contains any duplicates.
5. Given a non-empty array of integers, every element appears twice except for one. Find that single one.

1. Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

2. Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

3. Given an array, rotate the array to the right by k steps, where k is non-negative.Could you do it in-place with O(1) extra space?

4. 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.

5. Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?