How is everyone holding up? The last few weeks have been completely unpredictable in the worst ways. So I can only hope that you are all maintaining your sanity, continuing the job search and making progress towards your goals step by step.
I took some time off from blogging to recenter in the midst of this craziness. But found myself wanting to blog about my progress with algos and understanding Big O Notation aka time/space complexity.
Found this YouTube playlist by KodingKevin which does the best job I've seen in breaking down this difficult subject. I've been working through the problems in this playlist one by one. Pausing the video to attempt the challenge, reviewing his solution and quizzing myself on the complexity before hearing Kevin's answer.
I just finished the Capitalize challenge with a different approach. Check out Kevin's solution and take a look at mine below. How could I make my solution more efficient?
Challenge:
- Write a function that returns the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
My initial questions (important in an interview setting):
- Do I want to use Title Case (every word)or Sentence Case (first word only)? Response: Title Case
- Do I need to be grammatically correct? Make sure "I" is always capitalized? Make sure "the", "and", etc are never capitalized? Response: Ignore grammar rules
My solution (pseudocode):
- Split string into an array of words
- Convert every first letter to uppercase
- Convert my words array to a string with .join()
- Return the new string
My solution (JavaScript):
function capitalize(str) {
// Split string into an array of words
const string = str.split(" ");
const cased = [];
// Convert every first letter to uppercase
string.map((word) => {
cased.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
});
// Convert my words array to a string with .join()
return cased.join(" ");
}
Time / Space Complexity:
- O(N) aka linear -- this is because our solution goes through every element of our string