Google Coding Interview Question and Answer #1: First Recurring Character
Skills:
Algorithm Basics80%
Key Takeaways
Finds the first recurring character in a given string using iteration
Full Transcript
here's a coding interview question from Google this is a fairly simple problem so it's probably a sort of question that's often asked on the phone interview and here's the problem you're given a string and you're supposed to write a function that takes the string and returns the first recurring character in that string so for example if you're given this string as the input ABC a there's only one recurring character in the string a so your function should return a if you're given this string BC ABA you see that there are two recurring characters B and a and the first recurrent character is B so you sort of return be from your function if you're given this string ABC there is no recurring character in this string so your function should return either now or none depending on the language that you're using now for practice I do recommend that you pause the video right here think about it for a second and then come back to the video now when you're given this problem initially you may come up with a naive solution like this one in our naive solution we're going to first check the first character in the given string for example this string on the screen the first character in the string is d and we're going to ask ourselves is there another occurrence of the same character D in the subsequent characters we can check this character by character and the answer is no there's no other occurrence of D so we can move on to the next character B and then we can go through the same process ask ourselves is there another occurrence of B in the subsequent characters check that character by character and once we get to this other occurrence of B we'll know that B is the first recurring character in the string so we can just return B from our function and we're done with this naive solution we're essentially checking every potential pair of characters in the string to see if there is any recurring character and so the number of pears we need to check with this naive solution would be and choose two or n times n minus 1/2 which is equal to Big O of N squared and so the time complexity or the run time for this algorithm would be Big O of N squared let's see if we can do any better than that and here's a more efficient solution to this problem instead of checking every potential pair of characters we're going to go through this string only once from left to right character by character and as we see a character we're going to store it in a data structure such as a set a dictionary or hash table to show that we've seen this character already so for example when we're examining the first character D we're going to store it let's say in a hash table and in a hash table we need a key value pair so we're going to use the character as the key and the value could be anything but let's just use one here to show that we've seen this character once and when we go to the next character in this case B right here we need to first ask ourselves have we seen this character already before this character and we can do that with this hash table and the answer is no because B is not in this hash table yet so we're going to put B in this hash table as the key and the value will be 1 again to show that we've seen this character once we'll keep going like that until we get to a character that we've seen already which is the second occurrence of B right here in this case and we'll know that we've seen this character already because it's already in the hash table of course and at that point we can return B in this particular case from our function and we're done with this solution because we only go through this string once from left to right the time complexity or the runtime for this algorithm is Big O of n where and is the number of characters in the string now let's see what this solution by look like in code we're going to define our function first recurring which takes the given string of course for example this one and we turns the first recurring character in that string we're going to use suit code to explain this code the first step in our function is to define counts which is the dictionary or the hash table we're going to use to keep track of each character that we've seen and then we're going to run a for loop for each character in the given string or fork our in given string and then if this character char is already in the dictionary or the hash table then that means this is the first recurring character so we turn that character and if not for example if we're examining this first character in this particular example D put that character as a key in this dictionary or hash table and the value corresponding to that will be 1 and again this value could be anything but I'm just using one to show that we've seen this character once and here you might notice that I'm not using an else statement here but that's just a style issue so you could use it if you'd like to now after this for loop if we haven't returned yet that means that there is no recurring character in the string so in that case either return none or no depending on the language and that's my solution alright guys thanks so much for watching this video one interesting variation to this problem is to find the first non recurring character in the string instead of finding the first recurring character I covered that problem and many other problems in my brand new udemy course 11 essential coding interview questions in which I cover 11 of the most essential coding interview questions to master for your next coding interview there's a discount code below in case you're interested in taking the course ok thanks so much and I'll see you soon
Original Description
Find the first recurring character in the given string!
A variation of this problem: find the first NON-recurring character. This variation problem and many others are covered in my Udemy course, 11 Essential Coding Interview Questions: https://www.udemy.com/11-essential-coding-interview-questions/?couponCode=GOOGLE1
(You'll get a discount through the above link.)
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 14 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
▶
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
The AI boom broke the memory market, and the bust could be brutal
The Next Web AI
Bosses want you to use AI. Then they credit the AI
The Next Web AI
Australia’s AI copyright fight now has a datacentre price tag
The Next Web AI
AI, the Future of IT, and Why I Believe the Industry Is Heading Toward Another Cycle
Dev.to · Boris
🎓
Tutor Explanation
DeepCamp AI