My DSA Journey: Consistency to Conquering Data Structures and Algorithms
As part of my goal to become the best in my field, mastering Data Structures and Algorithms (DSA) is essential. This journal will document my journey through DSA, sharing problems solved, challenges faced, and key learnings.
So I have started with solving questions on leetcode, there's a Top Interview 150 questions which I am solving currently.
Firstly Solving Questions in the Array / String Category as this is the main category from which questions are asked for IOS devs.
Came across the 5th question Majority Element,
So the question is given an array nums
of size n
, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋
times. You may assume that the majority element always exists in the array. I had solved this already earlier as follows.
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
return nums.sorted()[nums.count/2];
}
}
But I didn't Liked the straight forward way so I came up with two other solution for this, while searching for it I came across
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
var dict:[Int:Int] = [:]
for num in nums {
dict[num] = dict[num] == nil ? 1 : dict[num]! + 1
}
return dict.filter {$0.value == dict.values.max() ?? 0}.first?.key ?? 0
}
}
Boyer-Moore Voting Algorithm — Finding the majority candidate given the element occurs more than n/2 times.
which works in O(N) time complexity and O(1) space complexity
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
var candidate = -1
var votes = 0
for num in nums {
if votes == 0 {
candidate = num
votes = 1
} else {
votes += (num == candidate) ? 1 : -1
}
}
return candidate
}
}
Rotate Elements By K times
Observed that we can achieve this by reversing the array one time, and then reverse the first k elements and then reverse the remaining elements
func rotate(_ nums: inout [Int], _ k: Int) {
let k = k % nums.count
if k == 0 {
return
}
// Reverse the entire array
nums.reverse()
// Reverse the first k elements
nums[0..<k].reverse()
// Reverse the remaining elements
nums[k..<nums.count].reverse()
}
Another method -
To find the split position and then slice and concatenate to find the final rotated array, ofcource don't forget to add the guard as the first statement to avoid unnecessary calculations.
class Solution {
func rotate(_ nums: inout [Int], _ k: Int) {
guard nums.count > 1, k > 0, k % nums.count != 0 else { return }
let positions = nums.count - k % nums.count
nums = Array(nums[positions...] + nums[..<positions])
}
}
Matrix Elements Sum, In my latest coding adventure, I encountered a delightful mishap while solving a problem on CodeSignal. I aimed to mark rooms below haunted rooms as unsuitable, but my logical error ended up perfectly matching the problem’s requirements! Read the funny and insightful story of how a mistake turned into the perfect solution.
Read the full story here: The Haunted Room Saga: A Funny Coding Mishap
30 days of Code Challenge, Happy to Share, Past Few Days(3–5days) I was on HackerRank Solving it. Tried to get through it quickly, used to try on my own if there's the swift language available as I am IOS dev, and for difficult questions, I used ChatGPT “teach me in easy language and use diagrams if required then used to paste the entire question” this helped to understand it quickly instead of just trying to understand by own fighting the grammar of the problem 😆.
I approached my team lead regarding the challenges and difficulties I am facing in an interviews, he introduced me to neetcode.io, where I found the same “Valid Anagram” question that appeared in the IBM online test. I’ll be focusing on that for now.
Remember, consistency is key!
For individual questions or related topics that I find exciting, I’ll be posting them as new stories to avoid this blog growing too cluttered.
This DSA journey is part of my broader goal to become the best in my field. Follow my overall progress in my main journal here.
Stay tuned for regular updates, and feel free to share your thoughts and advice in the comments.