Coding Interview Question and Answer: Longest Consecutive Characters
Skills:
Algorithm Basics70%
Key Takeaways
Solves the longest consecutive characters problem using coding interview techniques
Full Transcript
You're given a sequence of characters or string. And the problem here is writing a function that takes this string as the input and finds the longest subsequence within the string consisting of a single character. In this particular example, that's tripleB right here, which is longer than double A or double D, for example. And the return format of your function should be depending on your language either a hash table or a dictionary with a character as the key and the length as the value. And if you want to practice, pause the video right here and see if you can solve it and test your solution within 10 minutes. So how would I solve this problem? The key here is to go through this string only once from left to right so that we can solve this problem in big of n in time. And as we go through this string, there are only five variables that we are going to keep track of. Current for the current character that we're examining. Max count for the length of the longest sequence that we've seen so far consisting of a single character. Max car for the character that sequence is consisted of. Perf car for the previous character before the current character. And count for the length of the current sequence that we're examining. So let's say we're looking at the third character B. Current is B of course and max count is two because the longest sequence that we've seen so far consisting of a single character is double A right here and the length of that is two. Max car is a because that sequence is consisted of two A's. Pref is a because the previous character to the current character that we're looking at is A. count is one because that's the length of the current sequence of a single character that we're examining which happens to be here just a single character B. If instead we were looking at this B near the end count would be three because that's the length of the current sequence that we're examining and we'll be able to replace max count with three and max car with B. And when we reach the end of the string, we're going to use max count and max cure, which are three and b in this example to return a dictionary or hash table with B as a key and three as the corresponding value. Let's see what this solution might look like in code. We're going to write a function called longest which takes seek as the input and seek is of course the given sequence of characters. And just for convenience here, I'm going to assume that seek is always non-MPT. And the return value from this function will be a dictionary or hash table with the length of the longest sequence with a single character in this case three as the value and the corresponding key will be that character. First thing we need to do in this function is we need to initialize some variables max count, max card and pref card. It doesn't matter too much what we initialize them to. After that, we're going to run a for loop for each character, which we're going to call current in seek. In the for loop, we'll first check if the previous character is the same as the current character that we're looking at. For example, if current is the second d in the string, then it's the same as the previous character. So, in that case, we're going to increment count by one. If that's not the case, if the previous character is different from the current character that we're looking at, for example, if we're looking at the first D right here in the string, then we're going to reset count to be one. So when we're looking at the first D right here, we're essentially examining a sequence of a single character and the length of that is of course one. So count will also be one. But when we get to the second D, we're looking at a sequence of two D's and the length of that is two. So count will be two. If this new count is larger than max count, which is the largest count we've seen so far, then we're going to update max count to be count. And we're going to also update the corresponding character max car to be the current character. And after that, we're going to update per card to be the current character. And at the end of the function, of course, we're going to return a dictionary with max car as the key and max count as the value. That's my solution to this problem. And thanks for watching.
Original Description
Given a string, find the longest subsequence consisting of a single character. Example: longest("ABAACDDDBBA") should return {'D': 3}.
*Just getting started with coding interviews? Check out my "Get Ready for Your Coding Interview" course on Lynda.com:
https://www.lynda.com/Software-Development-tutorials/How-Ace-Developer-Interview/576698-2.html?lpk35=9181&utm_medium=ldc-partner&utm_source=CMPRC&utm_content=524&utm_campaign=CD20605&bid=524&aid=CD20605
(You'll get a 30-day trial with the link)
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 10 of 60
1
2
3
4
5
6
7
8
9
▶
11
12
13
14
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
📰
📰
📰
📰
Chrome Needs Twice-a-Week Patching Thanks to AI Bug Hunting
Wired AI
Stop Scrolling. Start Living: The One App That Finally Made My Life Make Sense
Medium · Data Science
Building a Production Expo App Using AI Tools
Medium · ChatGPT
The Most Underrated Python Skill for Freelancers
Medium · AI
🎓
Tutor Explanation
DeepCamp AI