Category: Interviews

Implement Merge Intervals in Swift

Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4]

Continue Reading →

Find the largest number by swapping adjacent digits if they have the same parity in Swift

Problem description: You are given a number. You can swap two adjacent digits, if they are both odd or even. For e.g. given (3,8), you can’t swap them. But you can swap (5,9) since they are both odd. They have the same parity. A similar problem is explained here https://www.youtube.com/watch?v=qEIGhVtZ-sg Function description: swapDigitsAndGetLargestNumber(number: String) Returns:

Continue Reading →

Word ladder implementation in Swift

https://leetcode.com/problems/word-ladder/ This post contains the implementation for the leetcode problem shown in the link above. A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> … -> sk such that: Every adjacent pair of words differs by a single letter. Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.

Continue Reading →

Permutations of a string in Swift

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique.

Continue Reading →

Magical Candy Bags

Magical Candy BagsYou have N bags of candy. The ith bag contains arr[i] pieces of candy, and each of the bags is magical!It takes you 1 minute to eat all of the pieces of candy in a bag (irrespective of how many pieces of candy are inside), and as soon as you finish, the bag

Continue Reading →

Element Swapping

Given a sequence of n integers arr, determine the lexicographically smallest sequence which may be obtained from it after performing at most k element swaps, each involving a pair of consecutive elements in the sequence. Note: A list x is lexicographically smaller than a different equal-length list y if and only if, for the earliest

Continue Reading →

Struct vs Class in Swift

If you look at structs and classes in Swift, they seem similar, but the most fundamental difference between them is how they handle values passed to them. In general Struct is pass by value, whereas Class is pass by reference. I will explain the difference by showing an example. In the above code, I have

Continue Reading →

76. Minimum Window Substring in Swift

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string “”. The testcases will be generated such that the answer is unique. A substring is a contiguous sequence of characters within the string. Example 1: Input: s = “ADOBECODEBANC”, t = “ABC”

Continue Reading →

Largest Triple Products in Swift

Largest Triple Products You’re given a list of n integers arr[0..(n-1)]. You must compute a list output[0..(n-1)] such that, for each index i (between 0 and n-1, inclusive), output[i] is equal to the product of the three largest elements out of arr[0..i] (or equal to -1 if i < 2, as arr[0..i] then includes fewer

Continue Reading →