Coding Problems Part 1

Joseph Harwood
3 min readJul 19, 2019

--

This week I made it a focus to go through LeetCode and do practice problems to prepare for interviews. LeetCode is a great place to keep your problem-solving skills fresh and practice your coding languages of choice. I am focusing on Ruby and Javascript in the below problems. It was an interesting exercise to see the subtle differences in how to solve the same problems between the two languages.

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Ruby:

def two_sum(nums, target)
hash = {}
nums.each_with_index do |num, index|
hash[num] = index
end

nums.each_with_index do |num, index|
diff = target - num
if hash.key?(diff) && hash[diff] != index
return [index, hash[diff]]
end
end
end

Javascript:

var twoSum = function(nums, target) {
let numObject = {};
for (var i = 0; i < nums.length; i++) {
let thisNum = nums[i];
numObject[thisNum] = i;
}
for (var i = 0; i < nums.length; i++) {
let diff = target - nums[i];
if (numObject.hasOwnProperty(diff) && numObject[diff] !== i) {
return [i, numObject[diff]];
}
}
};

Analysis:

I made a hash/object to keep track of the elements in the array and their index. I then check if the difference between the numbers exists in the numObjects hash and if it adds up to the target. I return the indices of the two that add up to the target.

Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Ruby:

def reverse(x)
limit = 2147483648
abs = 1
if x < 0
abs = -1
end
n = x.to_s.reverse!.to_i
if n > limit
return 0
else
return n * abs
end
end

Javascript:

var reverse = function(x) {
const limit = 2147483648
const k = x < 0 ? -1 : 1;
// let n = x.toString().split("").reverse().join("")
let n = Number(String(Math.abs(x)).split('').reverse().join(''))
if (n > limit) {
return 0
} else {
return n * k
}
};

Analysis:

This solution is pretty self explanatory, but the interesting thing here was that Javascript would return NaN when I tried to do the same Ruby solution where I set a variable to -1 or 1 and multiplied(it’s bold in the comments). I had to use Math.abs in order for it to return either positive or negative.

First Unique Character in a String

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

s = "leetcode"
return 0.
s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

Ruby:

def first_uniq_char(s)
arr = s.split("")
arr.each_with_index do |ele, i|
if s.index(ele) == s.rindex(ele)
return i
end
end
return -1
end

Javascript:

var firstUniqChar = function(s) {
for(let i = 0; i < s.length; i++){
if (s.indexOf(s[i]) === s.lastIndexOf(s[i])){
return i;
}
}
return -1;
};

Analysis:

The interesting thing about this problem was learning that the index (Ruby) and indexOf (Javascript) method returns the first instance of an index and that rindex (Ruby) and lastIndexOf(Javascript) returns the last instance of an index.

Check Palindrome

This next one isn’t from LeetCode, but it is an interesting problem. Build a function that checks if a string is a palindrome.

Example:

// bbaaa => baaab true
// wkjqq => false
// ffeeaaf => feafaef true

Javascript

function checkPalindrome(input) {
if (s.length > 1) {
return s.split('').reverse().join('') === s
} else {
return true
}
}

Analysis:

I like this solution because it’s short and elegant. All I did was reverse the string and see if it is equal to the the inputted string.

Conclusion

This is a nice introduction to simple coding problems that are good building blocks for tackling more complex problems. The next part will be my attempts at more difficult problems.

Thanks for reading!

--

--

No responses yet