Разбор лайвкода в Т-Банк на 450к / Android собеседование — Transcript
Full transcript
- 0:00Hello everyone. I'm Artur, a mobile
- 0:02developer. In this video, I will break
- 0:04down a solution to a real interview
- 0:06task for an Android developer position
- 0:08at T-Bank. This autumn, an HR
- 0:10representative from the bank reached
- 0:11out to me. They were offering a salary
- 0:13range around 400,000 rubles. And I
- 0:15decided to go through the interview
- 0:16process. After the initial chat with HR
- 0:19and discussing my experience, they
- 0:20decided to fast-track me through a
- 0:22simplified process with just one
- 0:24technical interview. Other developers
- 0:26usually go through more technical
- 0:28stages. I want to show not just the
- 0:30solution, but also explain my approach
- 0:33to such tasks and break down the topics
- 0:35they cover. Live coding is becoming
- 0:39more common in interviews because
- 0:41companies want to see if you can
- 0:43navigate code fluently, test your
- 0:45knowledge of specific topics, and see
- 0:47if you can clarify requirements,
- 0:49explain your thought process, and
- 0:51directly solve the tasks given to you.
- 0:55In this video, I am analyzing only one
- 0:56of these real-world tasks. But my
- 0:58students have exclusive access to
- 1:00breakdowns of over fifty tasks from
- 1:03companies like Yandex, Avito, Ozon,
- 1:05Alfa, Wildberries, and other major big
- 1:07tech firms. Let's move on to the task I
- 1:10was given. What do we see here? There
- 1:13is a lot of poorly written code here.
- 1:16To start with, I'd like to understand
- 1:18the main idea, what's even happening
- 1:21here. Let's try to take a look at it.
- 1:24Well, from top to bottom, there is some
- 1:28"const" object with a constant index.
- 1:32There is a tag, there is a "UserData"
- 1:35class with an ID, name, and phone
- 1:38number. This all looks like fluff that
- 1:41will need to be fixed. Apparently, the
- 1:44central class here is...And judging by
- 1:46the lines of code, the largest one is "
- 1:49UsersHolderSingleton". Let's take a
- 1:51look at what's happening here. We have
- 1:54a companion object with an instance of
- 1:57this class. There is a "getInstance"
- 2:02method that returns this class directly
- 2:05. There are some fields, a "
- 2:08usersExecutor", and an "update" method
- 2:11that takes a user ID, their phone
- 2:14number, and formats it. There's some
- 2:18fluff here, and then it searches for
- 2:20this ID in the array. If found, it
- 2:23replaces the value with the new phone
- 2:26number. Okay. So, the main idea of this
- 2:30class is that it has an "update" method
- 2:33to change a phone number. And it holds
- 2:36users. So it's a singleton because it
- 2:40implements the singleton pattern, and a
- 2:43holder because it stores a list of
- 2:46users. And actually, the main thing
- 2:50it's responsible for is changing the
- 2:52phone number. So I would also call it
- 2:55an "updater" or "PhoneUpdater". Let's
- 2:59do that. "
- 3:01UsersPhoneUpdaterHolderSingleton".
- 3:04Robert Martin is crying somewhere. Well
- 3:06, it seems that we will have to fix
- 3:08everything here, as if we could just do
- 3:10it right from top to bottom. So, let's
- 3:12look at "const". Where is this used?
- 3:15Ctrl + F "const" is our best friend
- 3:17here. Aha. When iterating through the
- 3:21user list, we go through the indices.
- 3:24Well, we know that in Java and Kotlin,
- 3:27indexing is zero-based, so we should
- 3:30set the index to zero. And it seems
- 3:33unnecessary to move this to a separate
- 3:36constant. We can just write it like
- 3:38this and remove that. Next, since we're
- 3:42looking at this for-loop, we have an
- 3:44index out of bounds here because
- 3:47indexing is from zero to size -1. So we
- 3:50can write it this way instead. Or
- 3:54there's an until function that does the
- 3:57work for us and iterates from zero to
- 4:01size -1. Or we could make it even
- 4:05simpler and just write for (i in
- 4:07users.indices). Pardon my English. Next
- 4:13, we have a tag; where is it used? In
- 4:18the logger. And then it's unclear why
- 4:23this tag is at the top-level. It seems
- 4:27it could be moved into a Companion
- 4:30Object, directly inside the class, and
- 4:33replaced with...a compiler constant,
- 4:39const. And why is that necessary? Const
- 4:42is a compile-time constant, meaning the
- 4:45value is calculated at compile-time and
- 4:48substituted at the call site, which
- 4:51speeds up code execution at runtime.
- 4:55Okay, let's move on. We have a UserData
- 4:59class. Uh, we immediately see mutable
- 5:03properties in this class. Well, anyone
- 5:06who has worked with Kotlin at all knows
- 5:08that using data classes is a best
- 5:10practice. Well, why? At the very least,
- 5:13we get equals and hashcode for use in
- 5:15collections that rely on calculating
- 5:17hash functions from keys and comparing
- 5:19objects. Additionally, we get a copy
- 5:23method to create a new instance with
- 5:26modified class fields. And this copy
- 5:30lambda, which takes the old user ID and
- 5:33phone number to change and then creates
- 5:37a new instance, hints at that. And if
- 5:41we make all fields immutable, the class
- 5:43becomes immutable. And that means we no
- 5:46longer fear race conditions when
- 5:48changing class data. But actually, a
- 5:50data class is not a panacea for
- 5:52concurrency problems. If a class
- 5:55instance is stored somewhere and the
- 5:57instance changes, we still need to
- 5:58synchronize access to it. Let's change
- 6:02it to a data class. Uh-huh. Moving on
- 6:12to our main class. It is open, meaning
- 6:16it's open for inheritance, but inside
- 6:19this class, all methods and fields are
- 6:22final, so we won't be able to override
- 6:26anything by inheriting from it.
- 6:29Therefore, it's more appropriate not to
- 6:32make it open for inheritance and keep
- 6:34it final. Let's look further. We have a
- 6:37private constructor. Well, that's so we
- 6:39create an instance of the class in the
- 6:41getInstance method. Everything is okay
- 6:44here. Let's see what is passed to the
- 6:47constructor. Context and a repository
- 6:50are being passed. We can talk about
- 6:53passing the context separately, but at
- 6:56the very least, these fields should be
- 6:59private to restrict access, maintain
- 7:01the encapsulation of our key class, and
- 7:04avoid exposing unnecessary fields.
- 7:07Let's move on. It inherits from
- 7:11LoggerProvider, uh, apparently to get
- 7:16this value for the logger. Is it
- 7:20appropriate for PhoneUpdateSingleton to
- 7:23inherit from LoggerProvider? I don't
- 7:26think so. This is a classic question
- 7:29about the difference between
- 7:31composition and inheritance. In general
- 7:33, preference should be given to
- 7:35composition over inheritance.
- 7:37Inheritance should be used when classes
- 7:39have a clear "is-a" relationship. When
- 7:41inheriting, we take everything from the
- 7:44superclass. This can lead to having
- 7:46functionality that we don't need, which
- 7:48is a violation of the Interface
- 7:49Segregation Principle. With composition
- 7:53, however, we only depend on the
- 7:54functionality we explicitly call.
- 7:57Inheritance also breaks encapsulation.
- 8:00A good example is provided in Effective
- 8:03Java and Effective Kotlin, where a
- 8:05subclass of HashSet was created to
- 8:07simply count all elements added over
- 8:10time. We overrode the add and addAll
- 8:13methods there. In the add method, we
- 8:15added one, and in the addAll method, we
- 8:17added the size of the collection being
- 8:19added. It would seem logical, but if
- 8:22you call addAll on this HashSet
- 8:24subclass, the result will be twice as
- 8:27large as expected because, under the
- 8:29hood, the HashSet's addAll method calls
- 8:31the add method. This problem could have
- 8:35been avoided if our new set contained a
- 8:37HashSet instead of being its subclass.
- 8:40Also, composition is more flexible
- 8:42because you can inherit from only one
- 8:44class, but you can contain as many as
- 8:46you want. And with inheritance, we take
- 8:49everything from the superclass, which
- 8:50changes the behavior of all child
- 8:52classes. With composition, while we can
- 8:55certainly affect the behavior being
- 8:57used, we know exactly which behavior
- 8:58from the dependent class is being
- 9:00utilized. And, of course, we have tests
- 9:03written for that. If necessary, we can
- 9:05even mock the class being used.
- 9:08Therefore, I would much rather not
- 9:11inherit from LoggerProvider here, but
- 9:14instead pass it into the constructor
- 9:21and get the logger from the
- 9:24LoggerProvider. Let's figure out what's
- 9:28going on here with the getInstance
- 9:29method and getting an instance of this
- 9:32class. First of all, our getInstance
- 9:35method is not in a companion object,
- 9:38but in the class itself. And so, to
- 9:40call it, we would have to create an
- 9:42instance of this class somehow, and
- 9:44only then could we call the getInstance
- 9:46method. This isn't a static method, so
- 9:49it needs to be moved into the companion
- 9:52object. Okay, let's format the code
- 9:57right away. And furthermore, our
- 10:01instance is volatile. What is happening
- 10:04here? So, in it, we check that if the
- 10:08instance is empty, or rather nullable,
- 10:10we create it and only then return it.
- 10:14Actually, a race condition can occur
- 10:17here. If two threads enter this method
- 10:21simultaneously and the first one reads
- 10:23the instance value, it will be nullable
- 10:26. The second one will also enter this
- 10:28method, read the instance value, and it
- 10:30will be nullable. Both will perform the
- 10:33check and both will create an instance
- 10:36of this class, resulting in invalid
- 10:39data. But we want to create the
- 10:43instance strictly once. By the way, we
- 10:46need to change the class name here. And
- 10:49there is another point here: a "val" is
- 10:52passed to the method parameter. There
- 10:55is no sense in this at all. This code
- 10:57won't compile simply because we pass
- 11:01references to class instances into
- 11:04method parameters. And "val" is only
- 11:07used in the primary constructor to make
- 11:10the passed parameter a class property.
- 11:14There is no point in doing that here,
- 11:15yes. Regarding the race condition issue
- 11:18. How can we ensure that multiple
- 11:21instances of this class aren't created
- 11:24when using this method in a
- 11:26multithreaded environment? Well, the
- 11:29first thing that comes to mind is that
- 11:31we can simply mark this method as
- 11:33synchronized. What will happen in that
- 11:35case? Performance will suffer here
- 11:38because we want to create the instance
- 11:40only once, and for all other times,
- 11:42just read the value. In my opinion, to
- 11:47make this code more efficient, we
- 11:49should use a multithreading pattern
- 11:51called Double-Checked Locking. What is
- 11:54it for? It is for limiting access to a
- 11:57critical section, where access must be
- 11:59for writing based on some condition.
- 12:03And we want to perform this operation
- 12:04strictly once when this condition is
- 12:06met. And all other times, when this
- 12:10operation or check is not met, we don't
- 12:13want to perform any synchronization.
- 12:15What do we need to do here? We need to
- 12:17remove "synchronized" because it
- 12:19suffers, because it affects performance
- 12:21. What do we need to do after checking
- 12:23that the instance is equal? Watch
- 12:26closely. I am setting up a synchronized
- 12:29block. Here, I perform this check again
- 12:34. And only after that do I populate the
- 12:41instance. This is, in fact, the
- 12:43implementation of the Double-Checked
- 12:45Locking pattern. What is happening here
- 12:47? We perform the check for the first
- 12:49time without "synchronized". If this
- 12:52check passes successfully, we then
- 12:55start the critical block. And in this
- 12:58critical block, we check our condition
- 13:01once more and perform the required
- 13:03operation. It looks a bit strange, but
- 13:07it is the Double-Checked Locking
- 13:09pattern, specifically so that we write
- 13:12only once and read without the
- 13:14synchronized block all other times. In
- 13:18fact, this pattern can be used not only
- 13:21for a GoF singleton. It is needed in
- 13:25general to limit access to a critical
- 13:27section based on some condition to
- 13:29prevent race conditions. This can be
- 13:32useful. For example, you have some kind
- 13:36of interceptor for your OkHttp. You
- 13:40want to refresh your access tokens
- 13:42there if they expire. And if you put "
- 13:45synchronized" on the entire intercept
- 13:47method, you won't have parallel
- 13:48requests. Each of your requests will go
- 13:51sequentially, which affects performance
- 13:53. And inside this interceptor, you can
- 13:56check that if you get a response saying
- 13:59your token has expired, only then do
- 14:01you enter the synchronized block,
- 14:04perform the check again, so that only
- 14:06one of the requests refreshes the token
- 14:09. And after that, you update the token,
- 14:12and this check of yours will not be
- 14:14passed, and you will execute the rest
- 14:16of your code, your remaining request.
- 14:18Okay, it seems we have sorted out
- 14:21double-checked locking. Let's move on.
- 14:24We have "users" and "executor" fields.
- 14:26Let's see if "executor" is used
- 14:28anywhere. Executor is not used anywhere
- 14:32at all, so it can be removed. What's
- 14:34next? Users. Here we get the value from
- 14:37the repository. And there is an
- 14:39important point here that, in fact, it
- 14:42works like this. And every time we
- 14:44access "users", we will get the current
- 14:47value from the repository. And this
- 14:50variable is mutable. First of all, this
- 14:53code won't compile because we can only
- 14:57create such a getter for an immutable
- 15:00variable. How appropriate is this at
- 15:03all? This field of ours is used to
- 15:07search for the ID we need. We get the
- 15:11value and then change this value in the
- 15:14list. But at the same time, this value
- 15:18changes locally and is not propagated
- 15:22to the repository to be written to disk
- 15:25or sent to the network. And it seems
- 15:30there is a mistake here in that if we
- 15:33change our users locally, then upon
- 15:36restarting the application, this data
- 15:39will be lost. I propose adding a method
- 15:44to the repository, an update method,
- 15:46which will update the phone number by
- 15:49ID, so that the data is current when
- 15:52the app restarts. Let's add the ID here
- 15:56and add the new phone number here. What
- 16:01do I propose doing here? We will
- 16:04receive the current list of users in
- 16:06the Update method. Aha. So, here I get
- 16:17the list of users. And when I find the
- 16:21right user, it turns out that "found
- 16:24user" is "users at i". And then we need
- 16:30to pass the updated user to the
- 16:33repository. I will also have an update
- 16:36method here. It will be "foundUser.id".
- 16:42The new phone number is "formattedPhone
- 16:45". That's it. We then remove this here.
- 16:49We are not modifying it locally. And
- 16:51this copy lambda is no longer needed.
- 16:55What other problems are there here? To
- 16:58format the phone, we access resources,
- 17:01apparently where some pattern is
- 17:03defined, and pass the parameter,
- 17:05actually, this phone number. In general
- 17:09, using context here can create
- 17:11problems for us. Why? It's important to
- 17:15ensure, at the very least, that this
- 17:16context is an application context. If
- 17:20it’s an activity context, then when
- 17:22the activity configuration changes or
- 17:24it's destroyed, we’ll have a memory
- 17:26leak because this class holds a
- 17:28reference to the activity. How can this
- 17:32be solved? Well, the simplest option is
- 17:34that when creating this instance, we
- 17:36can do it like this: application
- 17:38context. And we will solve the memory
- 17:41leak problem. But even if we solved the
- 17:44memory leak, the context tightly binds
- 17:46us to Android and prevents us from
- 17:48going Kotlin Multiplatform. Or we will
- 17:52have a problem where we need to mock
- 17:54the context during testing. Anyone who
- 17:57has mocked a context plus chat knows
- 17:58what kind of problem that is. Therefore
- 18:00, I would create a separate interface
- 18:03for phone number formatting. That is,
- 18:06we would have a PhoneFormatter
- 18:10interface with a format method that
- 18:14takes a phone number and returns a
- 18:18formatted phone number. And instead of
- 18:23context, let's use PhoneFormatter.
- 18:30Accordingly, we need to pass this
- 18:32PhoneFormatter to getInstance. And pass
- 18:36it when creating the class instance.
- 18:38And what will we do here? Format is
- 18:47format. What advantage will this give
- 18:52us? We will get more flexibility. We
- 18:55will be able to override the
- 18:56PhoneFormatter and fake this interface
- 18:59during testing. We are not tied to the
- 19:01context. And if we go to Kotlin
- 19:05Multiplatform, we can use expect/actual
- 19:08to do it for Android, iOS, and other
- 19:11targets. Therefore, I suggest leaving
- 19:14it this way. Let's move on. What else
- 19:16is confusing here? Let's see if there
- 19:18are any other problems in the code and
- 19:21if it can be improved or simplified. So
- 19:24, what is happening here? We iterate
- 19:27through the list of users, looking to
- 19:30see if there is a user with the ID we
- 19:32need, to change the phone number and
- 19:34pass these changed data to the
- 19:36repository. It feels like we want to
- 19:40simplify it a bit and do something like
- 19:44this. You can find the user simply via
- 19:49a lambda where the ID equals the ID.
- 19:58And if we didn't find the user, we can
- 20:01exit this function. In that case, we
- 20:04can format the phone number later so as
- 20:07not to perform unnecessary operations.
- 20:10Okay. Let's say we found the user,
- 20:13formatted the phone number, and then we
- 20:15call our update method. We call logging
- 20:19. Aha, here we change it to formatter.
- 20:24Okay. The code has become a little
- 20:27simpler. What other problems are there
- 20:29here? Our user repository returns a
- 20:32Linked List. What is the problem here?
- 20:36That is, the repository provides a
- 20:38concrete implementation of the list
- 20:39class externally. And for me, as a user
- 20:43of this repository, it shouldn't matter
- 20:45. It's just important for me to get
- 20:48that list. If we suddenly want to
- 20:51change the underlying Linked List to an
- 20:53ArrayList or CopyOnWriteArrayList, then
- 20:55usage sites will fail to compile if we
- 20:57explicitly specify the type during the
- 20:59call. If we start depending on specific
- 21:04linked list methods, like push or pop,
- 21:07and remember that the linked list also
- 21:10implements the Deque interface, clients
- 21:13will have to stop using those methods.
- 21:17All in all, it’s inflexible and also
- 21:19breaks encapsulation. Let's just change
- 21:22it to a List. Here, when we call it, it
- 21:25doesn’t matter which specific list it
- 21:27is. And for efficient searching, we
- 21:31don't want to iterate through this list
- 21:34every time, because that has linear
- 21:37complexity, but rather we want to check
- 21:40if a user is in our repository in
- 21:43constant time. And for this, a Map
- 21:46would be better suited than a List.
- 21:50Especially if we want to make the
- 21:52repository flexible; if it has some
- 21:54memory cache inside, a map is perfect
- 21:57here if we frequently update a user by
- 21:59a specific ID. Therefore, I suggest
- 22:03changing it to a Map, where the key
- 22:07will be our ID and the value will be
- 22:10User Data. So, what do we get from this
- 22:13? We get a map with a list of users.
- 22:19Integer, User Data. And how do we find
- 22:23a user? We can use users.get (ID). And
- 22:29if we don't find them, we abort the
- 22:31execution of this method. Then we
- 22:35format, and the repository itself will
- 22:37decide how to update the internal
- 22:40HashMap. That is, update the in-memory
- 22:43cache, or go out to disk or the network
- 22:45. And then, naturally, we log the
- 22:49result. Look at what other problems are
- 22:52here. After improving the update method
- 22:56, it seems like we don't need to make
- 22:59this class a singleton anymore, because
- 23:02nothing is being stored inside this
- 23:04class. There is simply no point in a
- 23:08singleton. We would just be keeping an
- 23:10instance of this class in memory
- 23:12constantly. Moreover, it doesn't even
- 23:15contain any data that needs to be kept
- 23:17up to date. So I suggest getting rid of
- 23:22the singleton. It makes no sense. Aha,
- 23:27we need to keep the tag. Accordingly,
- 23:30it's no longer a singleton. And it's
- 23:33not a holder because it doesn't store a
- 23:35list of users. So now it's just an
- 23:38updater by ID. It feels like this is
- 23:41already starting to resemble something.
- 23:44Yes, we also need to remove the private
- 23:46constructor so we can create it. It
- 23:49feels like this is now a simple class
- 23:51that we can use in our presentation
- 23:53layer to update a phone number. Let's
- 23:56just rename this. User Phone Updater.
- 23:59Use Case. Looks like that's everything.
- 24:02Actually, this code can be improved
- 24:05even further. In particular, in the use
- 24:08case—that is, in the business logic,
- 24:11in the domain layer—we are performing
- 24:14the user search ourselves and checking
- 24:17if that user exists in the repository.
- 24:20And it feels like that is not the
- 24:22responsibility of the business logic.
- 24:26In the use case, we just want to send a
- 24:28request to the repository, and the
- 24:30repository itself will decide whether
- 24:32it needs to update, search for a user,
- 24:35or create a new one. So I would suggest
- 24:39removing this from here, and we would
- 24:41just format the phone number when
- 24:44calling the update method, then send it
- 24:46to the repository. The repository will
- 24:49decide for itself what it needs to do.
- 24:51We also have a logger here. I don't
- 24:56think it's the use case's
- 24:57responsibility to handle any logging.
- 25:01This should be done either on the data
- 25:04layer side inside the repository, or on
- 25:06the presentation layer, for example, in
- 25:09the ViewModel. Well, it all depends on
- 25:12your business logic, so I also suggest
- 25:16removing the logging. Removing it.
- 25:19Removing it. Accordingly, the object
- 25:21companies, and we remove it from the
- 25:23constructor. I would also like to
- 25:25rename the repositories. Uh-huh. And
- 25:32the code has already become a bit
- 25:35cleaner. We've removed, well, about 80
- 25:38percent of the code. I suggest we wrap
- 25:42up here.
About this transcript
This page contains the full transcript of Разбор лайвкода в Т-Банк на 450к / Android собеседование by Фабрика Офферов / Артур Илькаев, generated from the public captions YouTube serves with the video. The transcript has 3,498 words across 527 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.