✓ Solution: Data structures interview

From the course: Nail Your Java Interview

  • Course details

    Have you lined up an interview for a Java developer position? In this course, review critical Java concepts-and commonly-asked interview questions-to ensure that you approach your Java interviews with confidence. Instructor Kathryn Hodge dives into a discussion of key topics that might appear as questions in an interview, such as the difference between the Java Runtime Environment (JRE) and the Java Development Kit (JDK). She also goes over string manipulation, data structures, and essential object-oriented programming concepts. Throughout the course, Kathryn offers examples that lend a real-world context to the concepts she covers.

    Instructor

    • Click here to view Kathryn Hodge’s instructor page

      Kathryn Hodge

      Kathryn Hodge works as a software developer at a media company.

      In addition to her work as a developer, Kathryn runs a YouTube channel for people wanting to learn how to code. Before graduating college, she worked for three years as a computer science teaching assistant. She is very interested in the intersection of media, entertainment, and technology, and is focused on creating software that makes an impact.

    Skills covered in this course

  • Welcome

    (soft chimes) - [Instructor] In the last video, your challenge was to find the most repeated word in a given string. Here's one way to solve that problem. First, we set up our test cases. Here, we have our first testString and we run the algorithm on it. And then our second test string which deals with penguins. And then our third test string that deals with students. Calling the algorithm here on line 107 and then our last test string which are the lyrics to a Maroon 5 song. And then we run the algorithm on it. But, what does this algorithm do? Scrolling back to the top of the file, we'll figure this out. Here on line seven, our algorithm is called findMostRepeatedWord. And, it takes in one parameter, a string, s. Then, going into the body of the code, we create a hashmap to keep track of how often we've seen each word. And this map's string keys to integer values. Next, we normalize the input. We lowercase s as well as trim off the extra spaces on the sides. Then, we go ahead and split this lowercase string into words using the delimiters we saw on the previous slides. Next, we go ahead and print out these words to the console so we have an idea of what they are. After seeing what they are, we iterate through the words array and we count how many times we see each individual word. Scrolling down to line 20, we go through each word. We make sure the word has at least a length of one. Then, we see if the word count map actually contains the word and make sure it's not a common word. Then, if that's true, we go ahead and increment the value that's associated with the key word. Otherwise, if the word does not exist, we go ahead and put it in with a value of one. Now, this else clause will insert common words into our word count map, but it'll only put them in once. So, they shouldn't be the overall most repeated word. Then, scrolling down to line 34, we find out which word was repeated the most times and how many times. We start off with this int max which is zero. This'll keep track of how many times we've seen the word and then we also have a variable for the word itself, the most repeated word. Then, on line 38, we go ahead and iterate through the word count map key set and as we go through each key, we get the value associated with that key, compare it to the max, and then, if it's greater than the max, we reset the most repeated word in the max to that current value and key. Lastly, we print out the most repeated word as well as how many times it's been repeated and then return the most repeated word. So, let's go ahead and run the function. Here, we see for the first text, the most repeated word was lorem and it was repeated four times. In the second text, we have the most repeated word, was penguins, repeated three times. In the third piece of text, we have the most repeated word as used, repeated four times. And then, the most repeated word is morning, repeated five times in these song lyrics.

  • Practice while you learn with exercise files

    Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

    Download the exercise files for this course. Get started with a free trial today.

  • Download courses and learn on the go

    Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

    Watch this course anytime, anywhere. Get started with a free trial today.

Contents