From Brute Force to Brilliance: A 7-Step Framework for Algorithmic Thinking - Rohit Bhardwaj
About this talk
This talk introduces a seven-step framework termed algorithmic AI thinking, designed to help engineers reduce ambiguity in problem-solving. The speaker emphasizes that understanding the problem thoroughly is more critical than rushing to code, as unclear requirements often lead to flawed solutions. Key issues in coding interviews such as vague prompts, hidden constraints, and early optimization attempts are discussed. The use of AI tools like ChatGPT and Replit is explored, showcasing how they can assist in identifying and correcting bad code. Ultimately, the importance of structured thinking, clarity, and continuously asking the right questions to shape requirements is highlighted as fundamental practices for both interviews and production environments.
Full transcript
Welcome everyone. So, as you can see, this is a seven-step framework which I have created. I'm calling it as, you know, algorithmic AI thinking. So, this thing, if you apply these patterns, you will be able to understand the problem really well. Secondly, how the people started to evaluate the uh in the interviews also is a little bit changing. So, I'll discuss that part also as part of
our journey. Now, these are the links you might be useful for uh you to keep like when you're working on your project, so you can uh you can write it down and, you know, you look at them later on. All right. So, let's get started. So, what do we what what what are we doing from the brute-force perspective? So, this is where the real problem is not
coding. It is the ambiguity. When I say ambiguity, what does it means? It means that when I'm looking at a problem, when I'm looking at a problem, the strong engineers win by reducing the ambiguity in the system. So, it's kind of garbage in and garbage out. So, we're going to talk about what that means, garbage in garbage out. The coding interview production instant are often failing the
same reason. You did not understand the problem. If you understood the problem well, solution is easy. ChatGPT, you can use any tool, cloud tool, you can use cloud code, you can use to solve this problem. So, that's what we'll be discussing. Why we are still failing? That's something I'm going to discuss today. So, the first thing is solving the wrong You got the requirements, but really you
know, you didn't ask the right question. We didn't ask the right question to understand it well. That's number one. Missing hidden constraints. We are not looking at scaling edge edge cases, domain rules which are applying. So, if you understand those rules, it makes it easier. Optimization is too early. You're trying to optimize the code and you want to optimize it, but optimization will come after it's actually
there in place. Explaining from the flawed model is also very important like you know, there is a flawed model is there. How do I do it? So, that's where the smart engineers are failing in in the in in their perspective. All right? So, wrong framing, hidden trap, costly launch, and confident failures. You're confidently failing and production is happening which is wrong. That's that's the real problem which
is going on. So, interview fail because of vague prompts you're trying to generate. Vague prompt is going to give you shaky assumptions, brittle solutions, and expensive rework. And how do I really look at it? And that's where the bug comes in. Now, what is what I can say predict is whatever happening in the AI space, that's a job security for us. Because by next year, they'll all
be calling us to come back and fix the AI mess which has just been created. Because all the code generated is really a bad code and and nobody is really looking at, "Hey, code is generated, it's good. It works." If it works, it's good. And that is what I'm going to debunk today. So, we're going to talk about a movie. In this movie, we're going to first
look at the principles which are there. Number one principle, clarity. What is clarity is more important than optimization. Don't try to optimize anything before that. Correct Correct behavior is very important and you should be able to explain what's going on before you solve that problem. And AI is a thinking partner, not a shortcut as part of our work when you're working on this. So, what I'm going
to do is we're going to look at defining, how do I define the problem? How do I analyze a situation? How do I explore the potential problem solutions and implement iteratively this problem, okay? So that is what is my main goal for today, all right? How we do that? Through candidate funnel, ambiguity has to be resolved. So in an interview in any of the project you're working
on, try to remove the ambiguity. Clarity is power. I would call it that way. Now you establish the baseline, now you're optimizing the solution coming to you. AI is writing 70% of the code right now. How many here agree with me? 50% at least, you know? It's writing that code. What are the biggest AI worries you have? What do you guys think? What is the AI worry
you have? Huh? >> Your jobs. >> Taking our jobs? I'll tell you it's a job security right now because you're going to be helpful because because you're writing bad code to begin with. So that's the real problem which is happening here. Now why I'm saying that? Because there is because Replit is writing the code. When the Replit I'm not going to go there and click on this,
but there is a wipe coding incident which took place. Everybody probably know that production data was deleted. And it's not following the commands. It's doing its own things. The problem is not this. It was following the command 95% of the time. Just like I do, like I follow like 90% of person time of my spouse, you know, what she's saying. Sometime I don't find I get more
stuff than she wants, you know? And that's not good. And same thing is with the kids. Kids probably follow like 50%, you know? 50% they want to play video games. That's exactly what's happening here. What can I do to really prevent myself? We're going to discuss that today. Right? So what all that means? There are security holes, unsolid design, scalability unknown. So what's really happening is Copilot
is generating this code. JWT token hard-coded, and that is actually causing the developer commit this code to the GitHub. Now, it is available to the public and some other AI tool. Now, this JWT tool tool is used by others to really work on. That's the real problem. We're creating god classes. Some of these things are existing code. Doesn't matter. If this code is there, what do you
need to do to prevent it? That's the real problem we're going to discuss today. So, what do I need to do? You need to start the refactoring, understand the responsibility, verify the problem which are coming in, and then test it and refactor and solution Now, what does that really means? Uh before I move on, two people came little bit uh now, so I'm going to share again
the link for you guys. These are the links. We're going to go through very practically through these things. So, you need to capture these links. One is a tiny URL algorithm AI. We'll go through some exercises from Second link is the GitHub link for the which has a bad code in it. We'll discuss that bad code, how does that look like. Tiny The This is my agent.
tinyurl.com/algorithmicaiagent. You can use this agent to ask questions. Whatever knowledge I have for this talk and for your interview, everything is So, you can practice, you know, your projects using that also. Makes sense? Does that sounds good? Okay. So, now I'm going to go and and and take a look at that bad code and try to solve this code together, okay? if you go to the GitHub,
and this is what I'm going to open right now. So, we can actually see the code from here. Okay. So, this is the invoice service given given to us. This invoice service is there Are you guys able to see the screen? Yeah. Okay, this invoice service is there which is getting you know, JDBC URL uh database pass fail and then JWT token. I think this is doing
a lot of things here. If you can see it's trying to issue the token invoices are uploaded and sending the email. Now, if I look at this code, can you find out what are the problems in this code? Just looking at this code. I'm sorry. Yes, it's it's it's a bad code to begin with. It's like God classes created. Now, if I if you get a code
like this and you don't have time to really see what is in this code, you need to find how to fix this code. How many here want Say I. Yeah, I want that, you know, I want I'm lazy, you know, I don't want to work. Uh you know, it's like, "Hey, give me what the problem is." Okay, let's do I'm just going to do this with you.
So, I'm just show you what I'm doing. Copy I'm going to go back again here to my chat GPT agent. So, this is my agent. This is the actual agent which I'm which I have created here. And based on this agent, what I'm going to do is I'm going to go to this particular agent, seven algo agent, and paste this. Apply the Apply this prompt to find
the issues. So, I got a prompt here. I'm going to show you the prompt.md. I'm going to copy this prompt and then paste it here. I'm pretty lazy. Monkey see, monkey do. Everybody Somebody has done the work for me. I want to just use it. That's exactly what I'm doing here. Based on this analysis and I'm going to make it a little bit bigger for you, it
is able to find out find out issues in the code which is given above. I didn't even read Right? So, I can just go in and find out from this code, oh, there is a severity is high. God class is created. Single responsibility principle. Security issues are there. Hardcoding tokens are there. Security concurrency issue is there. Uh design problems are there. And and performance issues are there.
Design issues are there. And we get all these issues and solid is scored. And now we are able to find out what are the bad variables and what do I need to do to fix this problems? How many of this so far whatever you see? Now, you didn't even do much, but that's what the benefit of AI is. You need to know what to use. If If
I give you this prompt, I'm going to show you the prompt next. What am I doing in this prompt? So, it it makes it easier for you to understand what's going on here. Basically, this prompt is saying that, you know, is a staff level Java architect I am and make sure you go through solid principles, uh creation patterns, structural pattern, behavioral pattern, concurrency, security, performance, and all
these patterns. Output in this format. Based on this, now I can apply, you know, uh pattern radar and suggested solution. At the end, the analysis is done with the rules. All right? So, why I'm talking like this is the I want to before I talk about the steps we need to take to solve the problem. We need to understand where the problems are. Okay? So, we are
dealing with where the problems are first and I'll talk about the steps after this. Okay? That's the reason I'm starting with this because you want to make sure you can relate to, hey, Rohit solved a your industry, not this industry. I want to solve for whose industry? Your industry. Okay. So, the next thing is when I got the suggested refactored code also here. Now, invoice response repository.
So, what it is saying right now, it's saying that repository, create an interface for that, and and then based on that interface, create implement that interface with JDBC repository, and and then and then call through the main method. Basically, it's trying to create create multiple interfaces to solve this problem. That's what this is saying. So, let's see if I apply these principles, now I got the new
service created with the main method. In the main method, I create and I did dependency injection. I did dependency injection. Let me make it a little bigger for us you to see. Dependency injection for in-memory in-memory repository I'm calling here, and in-memory cache service as well as static. All three things I'm making a call to. Now, I can go inside this and then find out like how
this is using, or I can actually go in through my model and look at the invoice service. Invoice service is getting a dependency injection into into this. So, instead of instantiating it here, I'm instantiating it instantiating outside. I'm creating the invoice and saving that invoice and calling calling these methods. Behind the scene, I have a model created, and I have got different different cache ports created to
call them. Okay? So, the advantage, why are we doing this thing? We creating all these interfaces so that it becomes easier for me to use the system. All right? So, that's what we did based on based on the data which is coming to me. Let me run this up. Now, we can see that, you know, invoice is one. It's saying the invoice is one And 42.50 dollars.
Now, why this is so important? I'm so excited about this is because you are able to solve a concrete Java problem. So, you can take this for any Java code and be able to do that. Yeah? But the next thing you're going to say that, "Hey, I am in the financial industry." How many here are from the financial industry? Okay. So, I created a prompt for you.
Right here. And why this prompt is different, it's doing all these things, but it is also doing regulations which are related JCIP JSR 133. I think these are some of the uh the some of some of the prompts which are there from the financial industry perspective, you know, um security perspective is also there. And And if you want to create your own prompt, what you do is
that based on this prompt, what I'm going to do is based on this modify that for And give me another industry right now. What it Health care. Give me that for health care industry. And then we are trying to do I mean do the same thing with the with the health care industry and we'll be able to create the prompt for that one. Now, this is the
audit the health care and HIPAA compliance is more important in this case. Now, you're a developer a developer in health care industry. So, why this is important is that now a solid principle domain is added now in this one. So, domain is a health care domain. Patients and count you know, encounters and medication is there, clinical history is there, HIPAA compliance is there, HL HL7 I think
FHIR I think you probably know more than more than me on this one. So, basically you going to get this prompt and you can use it for your for your purpose. Okay? So, that's the benefit which we gain just by doing this simple thing. All right? So, why I started with this is because now AI suggest suggest certain changes and and and then Black Friday comes and
then we found out that oops, it cannot scale. We need to fix that in an emergency mode and then do it. Instead of the emergency, you have a prompt to really fix it properly. Okay? That's the reason you know, we we we're talking about this. So, we'll discuss this more. So, AI success is not AI safety. So, that's where the solid principle comes in place. So, the
guard class is there. You want to be able to create the JDBC and other classes and create the interfaces to solve the problem. Okay? So, I'm not going to go through everything, but I think I showed you the code what what's happening there that to get the perspective. Let's talk about the seven steps now. I started with the end in mind. What is our North Star looks
like? But what are the steps for cracking the AI algorithm? That's the real question for us which is there. So, if I had 1 hour to solve a problem, I'll spend 55 minutes understanding the problem. Now, this is more important now. This is Albert Einstein saying it way back. Why this is more important? Can anybody tell me? Then you should develop. Now, earlier what you used to
do was let me do small small thing and I'll find and refine it afterwards. With AI, that's not the right approach. Because because AI get the garbage, you'll get the garbage back. Yeah, you have a question? No, okay. So, that's that's the part we want to actually fix in this case when we are when we are working on it. So, what do that means? It means that
we want to understand the requirements, understand the quality and use the left and the right brain using the analytic skills. So, we're going to talk about what are the skills needed for us to do it, coding skills, technical skills, and communication skills to solve this problem. Yeah? So, while doing this, uh you know, algorithm has an input which has unambiguous input is there and feasible output is
there. This is what the algorithm is really Now, AI is making it non-deterministic also. What is our job is to make AI deterministic? Why, Rohit, you want to make it deterministic? Can anybody tell me? Why are Why are we moving towards more determinism in the AI space? Ownership. You can trust like, you know, hey, it will do the right if I'm telling it to build build uh
build a house, it should build a house, you know? It should not build a road for me, you know, it's not going to help me out. That's exactly true. So, we are responsible to make sure we have well-defined input and output coming in. Now, AI, the problem is not in the AI. Problem is the kind of questions we are asking. Okay? So, asking the right question will
take you to the next level. So, asking these question, what are the input and output, you know, what is a tree and graph looks like, expected output looks like, what are the constraints which are there, size constraint, different different constraints, and then what's the specific run time requirements you might have, what's the space requirement is there. Now, sometime you have a bigger set data is there, you
want to have a space limitation also there. Um you want to keep that in mind. And are there any edge cases? Now, these questions are not only response required for you to ask when you are in with talking to project manager. It's required in interview, also. I have like, you know, a boot camp on on this. The most important thing Google is looking for is how you
approach a problem. And same thing is to Anthropic and other ones like they change the way we are remembering all this code, but they are actually coming in and saying that hey, clarifying question if you're asking the spin-pointed question, that shows that you can work with ambiguity. Nobody will give you the requirement And they're going to just give you something fluff, but that's not what they need.
They're going to say, "I want this. I want this." But that's not what they need. Are there any duplicates which are there? Are there any specific data constraint which are going on? You got the idea. Is there any standalone there? Okay, so one more thing. Is it running standalone or we need to have extensibility created for myself? So those questions, when I ask these golden questions, now
I can take this to the next level. Now I can find out what are the problem which might be there which will be useful to implement with. Make sense? So asking these questions is important. The second thing important is that we want to make sure that there is no for minutes. One for minute is fine. They should not be two for minutes within your code. Okay? So
that is one another problem which is there. So but that's not the main thing. The main problem is your microservice is calling another microservice. Your AI is calling another AI is calling another AI. So how many for minutes are there? Guess what? Maybe n square for minutes. So you have to look at the holistic system. So it looks like hey, I just have one for minute, but
hey, within that it's doing another for minute. So you need to find out holistically what's going on to find out this problem, which is done through stack trace. When you look at the stack trace, then you can find out hey, how many times it's been called. So now you know if there is there is a real problem in this case to solve it. So that's where the
complexity comes and you want to avoid the n n n cube complexity, but anything above n is not So that said, uh you know, so that's that's the perspective which we need to know. And of course, anything which is more than n log n, you know, we have to be careful about. So, that said, brute force is there. Understand and verify. Based on that, the first bug
is usually understanding. That's what I was I'm trying to say here. So, what all that means? Before writing the code comes in, input output constraints comes first. So, that's act two. Act two is understand the problem, pinpoint the shapes. What are the shape? Time shape, ordering, duplicates. These things comes in and that is actually now making it production ready. Now, you add Can you Can you walk
me through a concrete problem statement which is coming in? Latency, memory, utilization. And that is where we are trying to reinstate the problem. Let me reinstate the problem again. Now, this is called as mirror technique. Mirror technique is used in interviews as well as it's used in understanding and clarifying the problem. Reinstate the problem. Repeat the same problem again in your own words. Confirm the constraints. Narrow
down the scope for your problem and align the align the solution. Once you do that, now you can frame the problem. Structured reasoning has to be created. Step number one, step number two, step number three, step number four. So, when I do this structured reasoning, then I do trade-off judgment and communicate what things needs to be done under pressure. So, once we know that, explain what are
the assumptions which are there and how it needs to work. Now, we need to understand application. They want to see algorithmic thinking, not not just solving a algorithmic problem. That's where algorithmic thinking is really coming in. So, why this is important? Because garbage in, garbage out. We need to resolve this garbage in, garbage out by providing weak prompt to actual better prompt. So, what I did was
I showed you the North Star. My prompt can find out No, is it my prompt? I iteratively work on it. Can you make it better? Yeah, but use that prompt to start with. Do not start to say that, "Hey, you know, create a code with ChatGPT without all those constraints in place." So, create a better prompt, and then list all these constraints to yourself, now you are
able to work on actual solution. So, do not code first, understand the problem, clarify the results, verify, and then code for it. And when we do that, then we are able to go towards the requirement shaping. So, this requirement shaping means that you have a problem type, input, output, constraint, assumptions, and problem type is defined here. That's where you're writing down checklist of this problem, and and
code in, you know, any of the tool which are there available to available for you. LeetCode is a great way to do that. You can practice the problem with that. Also, if you want to clean space. I never go to Leet and you know, go to ChatGPT first. I pseudo-code it in my on a piece of paper. Actually, that is better way of understanding the problem. Don't
go to ChatGPT initially. I'm not saying you don't go there. I put it on the paper on a piece of, like, you know, a blank sheet. I pseudo-code it what it looks like, and then I can go and look at the code how to solve it. Because if I pseudo-code it, I can delete it. I can rub it, and you know, "Hey, I don't like this way
of doing it." But if I give it to ChatGPT, it's going to solve for what I give that person to solve for. Make sense? So, that is one thing which is important. Another thing is components. And are they dependent components or not? Once you understand the component model for yourself, now you say, "Who's calling me? Who is Am I making a call to? And make sure that
interface is done and you use the creativity and logic both to solve a particular problem. You have to use both the brains. It's not just one brain to solve solve a problem and compress. Right now for AI engineers, they're actually looking for you have Like I have both the brains to solve. So, this is this is what this when I talk to see you know you know
CIO or like you know VPs, they're they're saying that I give you a problem. Don't give me what your what I ask you Surprise me with something more than what I'm what I'm asking you for. So, give me a better answer than what I've not even thought about. That's what they're looking at. They're not looking for a simple solution. And that's where we reinstate the problem, verify
the understanding, connect to the material which is given to us with the active listening skills. Now, this active listening skills are really important for us to understand. And that's where the existing code comes in. Understanding the existing code and algorithms and preserve all the interfaces which are given to me and solve for the algorithmic problem. What I did was I had a God class. I used a
prompt and it suggested me certain things to do. Now, I took an action and then I basically applied that prompt to solve my problem, okay? That's exactly what we need to do to do that. Now, what all that means? It means that solving lead code, a great place to get started on like you know, I think we all know this lead code perspective. But listening to every
detail is very important. I'm working on my listening skills at my home. It's very important for me. But that is what happens like when I'm trying to understand the constraints, I need to see what is the path for for a particular constraint. If you want to know about graph and you know how the graph is actually changing the world and the algorithm? I had a talk on
graph on the first day. Just watch that. You will get a better perspective on the graph and what kind of questions comes there from algorithmic thinking perspective. Because because world is moving towards more towards the graph. Okay? But then, you are your job is to understand the and avoid these constraints to solve a problem by asking the right questions. So, writing the questions is important and mimicking
the other person is more important. So, this diagram is See this? I'm encoding and then decoding it. Now, encoding is done using one hash. Let's say hash hash 25 and decode using is done through hash 50. What will happen? Would I get the right result? No, I won't get the right result. And that's the real problem everybody's facing right now. Product manager saying, "I want this." We
did not translate that into what they really need. And then, that's all solved for that problem. And once we do that, and the way to do it is a very simple six skill is there. If you apply that skill, which is called the slant slant skill is there. Sit up, look into the eye contact, ask the right questions, note taking, and then implement that. So, Rohit, why
are you talking about this in AI? We still need to work with an AI to understand this. Now, you're probably not sitting up and eye-to-eye contact with an AI. Until unless you're creating an avatar, then you can do that. I have like few agents which of my own, which are my own avatar. I talk to myself, you know, sometime. There. But really speaking, you What will happen
is that we will be talking to an AI to understand the questions well, because AI will start giving us the requirements also moving forward. But what would happen is when I do the mirroring technique, at least in the interviews or with the product manager, mimic the way that they are talking. Mimic I Love Lucy. How many have seen this? This is like old show which is there.
Nobody know about it, yes? I should not use this, but it's it's a it used to be very old show where they used to mimic each other. So, mimicking is a mirror technique which is there. Understanding the other person requirement. If you're writing a blog post, you need to understand the requirement. If you are in that domain, you need to understand the other perspective. Once I do
that and with the with also the gesture. See this, the way they are sitting right now here. So, I and then and and doing things. So, when I do that, what happens is that we are understand the other person really well. This will work for your kids. Go today and don't do anything, just mimic them. They will love you like anything. And you know, of course that
will work for anyone else in the home. And you know, I do practice this a lot. Uh but the problem is that that is the communication part very important. Finding out edge cases and different different ways of looking at this data with time and space complexity is going to take us towards the next level. Understanding the assumptions. Hey, these are my assumptions. Based on this the assumptions,
I'm solving this problem. Uh and then sometime these are the assumptions I'm not going to do it. So, sometime sometime writing them down is also uh very important to understand. And then and then draw specifically sufficiently large example. So, instead of solving for a problem, you have a large sample to work with. Sometimes you don't have a good sample to work with also. How many here say
that you have a production code? And your production have good data, I don't have a good data. How many here have that problem? Okay, I'll solve for it for you today. You know, we'll we'll discuss how to do that. But really speaking, brute force to brilliant the act three is read the read read the terrain, then build the brute force. So, it does not matter you solve
it through brute force. We are trying to optimize a problem instead of understanding the correctness first. So, that's the first thing we always do. Interview also this matters a lot. Inspect the terrain for the code. Understand the domain like existing code paths which are there current how what's the current behavior looks like for us. How they how it's currently working. What are the data what are the
failure modes which are there. I always look for failure modes. Now, simple problem is easy to solve. But failure mode is more important. So, whenever I work on a use case, this is something which I tell to everyone also. You just know this part is is going to change how we write stories. We all write stories, yeah? For creating creating something. But you know what? When I
write a story, does it have preconditions? Before you do this, you need to have What are the postconditions? And when you're writing a story, doesn't have exception flows? You just add exception flows. Preconditions and postconditions. Now, your story or a use case is complete which we call it as a scenario. Just by doing that exercise, data shape and understanding this, you have a better understanding of a
problem. Makes sense? Because what happened we solved it whatever they asked for, exception flows are missing. And that's where the real problem comes in. Question is what what already works, what is broken, what we cannot change for. You know, evidence which is telling us these things are important. Once we apply these principles before adding any code solving for the problem, understanding the problem is very Now, interview
mode versus production mode. Clarify the prompt. Interview mode you have to prompt it like nowadays, I think within a year people will be judging how what how good prompts we write. Simple. They will be judging how we write the prompt to write the code. You they will be using some kind ChatGPT or some other code to do that. They If you write the hey, I want to
solve this problem, enter, not a good idea. So, you have to make sure prompt engineering build the correct baseline, trade-offs you're adding to really solve this problem. This is the interview mode And existing code, production-grade code you need to understand. In that case, use my prompt, which I gave you. Use that prompt, and then you can give your zip file, it will analyze the whole thing. You
can use How many here use uh um GitHub Copilot? If you use that, use that prompt with If you ask GitHub Copilot for a given problem, it will solve it won't solve properly. You have to give proper instructions, then it solves for it. That's that's one thing I would suggest always to do. Build brute-force baseline. Don't try to solve for it, like, you know, correct brute-force is
going to help you out out build the solution. Okay, so let's take a look at an example for this, two sum evolution. So, let's let's solve for this this particular problem together, how we do the do this problem, okay? So, brute-force is the first one we're going to look at. Okay, this is the problem given to you. Check for a pair of array in a given sum
of array. Okay? And do that. How would you solve this problem? Anyone? >> I think that I think so. >> Okay. N squared, yeah. N squared is good way to do that. Okay, anything else? >> The head? That we can do in order of n. >> Order of n you can do that. Okay, that's also good. Anyone else want to talk on this how we Sorted array
or a normal? Awesome question you are asking. See, what he did ask is see the main thing is this is what happens. We coming to solution mode. N square, N, N log N, we go in that mode. What we need to do is The whole thing which I did before was to understand, clarify the requirements first. So, if you just do that and don't answer the question
first. Awesome. That's what we are missing in in our in our puzzle. Solving this problem is very easy. Asking the question, the easy the arbitrary range. Is it negative numbers and positive numbers which are there? Are there negative Yeah, they can be negative, positive, or zero numbers can be there. Can there be duplicates? Yeah, maybe there could be duplicate in this one. Uh or on these things
can really help you probe what's going on. Now, these probe questions we I'm trying to ask. I'm spending like 5 minutes I'm spending more time on this, but this is very important I'm discussing. Just by doing this exercise, you are increasing your chances for clearing the interview. Okay? Not only interview, in your office also this is going to really help you um understand what's going on. Now,
because I know it's a negative number there, now I added the negative number in this one and output is added with this one and then we are able to work through the pair and then solve for it. Okay? That's what I did. Uh and then based on this, what we're going to do is you're going to sum pair. Now, brute force is N square complexity. We are
able to do that. Uh same thing is to is sorting. You can also do sorting as easy, yeah? So, we all can do sorting here. What is sorting means? What's its complexity level? N log N, yeah. We all know that that's N log N complexity is there. You sorting takes most amount of time and then you do the binary search. Binary search is another good way to
do that. So, n log n complexity. But, if somebody say there are there are billion of these items are there, then what are you going to do? You still need to sort. How are you going to sort it? Heap sort. Then you're going to pick the heap sort to solve this problem. So, the reason I'm saying this is like this is these are general thing which are
there and use hash table is another way to do this. So, hash table I can apply the hash table and I'm putting in the hash table and if that value already exist, I'm just I'm just getting here. Here's the pair pair has the value k minus one and if it's already existing, yeah, then that's what the value is that that comes in through and then we are
able to work through it. Great. So, that said, these are all the thing which we can do. Now, sum equals k. Okay, let's take a look at one problem on this one. Uh the the reason I'm showing you this because I want to use the agent now. Equals k. Lead code. So, lead code has sum equals subarray with sum equals k, yeah? Yeah, so this is the
problem sum equals k. So, this is the problem which we have got. Now, what I'm going to do is What's going on? Okay. So, I'm going to copy this. I'm like lazy person, you know? So, I want to copy this thing and then I'm going to go to my go to my seven seven step layer, you Apply the seven steps to this problem. Okay? Because I want
to make sure that makes it like very easy to understand. Understand the problem well. Array is there. Clarifying question. Now I'm asking the clarifying question to it like you know in negative numbers, sliding window sliding window not important. I need a count. Now I'm able to solve this through Python or some other some other way to do the same thing, okay? So that's the optimization with a
hash map we are able to do and we are able to create the solution for it. Validate the edge cases. Now I'm going to do the edge cases for it to solve for it and and and and and bring the solution. So the final solution is and do the same thing. Okay, let's say. Step by step. Do the same thing in Java with step by step. If
you just apply that formula, so understand the problem and you know and then and then brute force to solve this can also come through and then while we are working on it, we can build the solution. Now on the same line, if you have been given a problem to solve and you need to find out which one to pick. How would you do that? Anyone? So you
have you have multiple choices to pick. You know, I say hash table and other ones we can pick here. So what you can do is you can do apply chain of chain of three three reasoning for above solve for and and give comparison. So what I just did was instead of me looking at hey, what are the different ways to do that? Say if sum equals to
K, now we are doing a sum prefix sliding window, okay? So these are the three things which we did to solve this problem. Approach with branch one. Pros and cons, branch two. Pros and cons, sliding window. Pros and cons. And then at the end, tree map and ordered map also we can do here. Now based on that, we found out that tree map is n log n
complexity. Prefix and hash map is the best solution which we can get O of n complexity in this case to solve this particular problem. Does that make sense? So this is another way of finding out hey, because a problem can be solved in multiple ways, yes? You need to now defend yourself. Why you pick this approach? Yes? So in order to defend Now I'm talking about uh
you know, a senior person ask you a question, hey, why did you pick this approach? You know, why are you trying to pick this approach? What can you do? Say you say tree of thought process and give me the options and comparison at the end. Just by doing this approach, now we know which one that we need to pick, hash map. Yes? Similar to this, I can
pick any problem and solve for this, okay? We'll solve some more problems similar to that, but I want to just spend time on this and understand how to do it. Now we are need to understand the pattern recognition. Now this is where the pattern recognition comes in play. We need to understand instead of solving this list of pattern, we need to What vision should I pick? Two
pointers, sliding window, depth first search, heap, dynamic programming, greedy, which one should I use? How do I approach this problem? Because this is the something which comes in. You can apply tree of thought process also here to solve this through our linkage. Now brute force to brilliance is pattern recognition comes in play. Now you're looking at instead of not memorizing 200 solutions given to you. Now with
the AI, you don't need to do that. Yes, you agree with that, yes? Now just by applying the principles, you'll be able to do it. Now you need to move from coding symptoms to spotting the patterns. You're looking at a pattern. Maybe it's somebody is asking for a search. I need to use this pattern. So, we're going to talk about different way techniques to solve this problem,
okay? So, pattern recognition can be there before selecting an algorithm, ask these questions. So, what are the questions I'm going to ask? Need fast look up? Easy, hash map solution, Uh need to narrow down the sorted sorted, uh you know, search space, binary search, yeah? Uh need to maintain the continuous region, sliding window. Like you there's a sliding window approach is there to solve a particular problem
in this case. Need the shortest path? Because I'm asking for shortest path, we're going to use breadth-first search to solve solve that particular problem. Okay? And then, need dependency ordering? Then topological sorting can be done. Now, this is like somebody saying that do this activity and then do this activity. So, it's like dependency graph you're trying to create. In that case, you can use topological sort in
this case to solve this problem. So, do not ask only what algorithm do I remember? Ask what structure is hidden in this problem? That's the real thing which we'll be looking at. Okay, let's take a look at this for a second. Uh then we'll be in a good shape, okay? Take a look at this. Now, why I created this is this are homeworks, by the way. You
can look at this and apply it in your project. So, you can say structure is hidden. So, you are looking at signals in the problem. Let's take a look at one of them. Need Binary search. Shortest path is depth-first search. If I'm looking for a shortest path, I'm really good at breadth-first search, which we can do in this case. Um now, now another one is, you know,
does this path exist or not? Is depth-first search, which you can use. Branches exist or not? Is a depth-first set. Priority queue, what's the best next item to to call? What's the what's on the top? That's called priority queue. Um which we can use. Two pointer pointer problems can also be there. Same thing with merge interval can also be there in this case. So, you have been
given a problem, how would you approach Okay, that's the question I'm trying to ask. So, you're asking these question. What's the real input shape? What's the real output shape? And based on this, is there any repetition or not? And then you're planning to find out the patterns within this. What's the best pattern to really solve this problem? Let's take a look. Fast look up. Fast look up
is it has table. Yeah, is is a good way to do that. Fraud rule checks. Discount eligibility. So, these are all fast look up problems are there. So, now I'm practicing, this is the one. So, instead of you doing what you need to do is and and I'm giving you this prompt, so you can just take it, you know, copy it and just run this prompt, okay?
And you will get a better understanding of like, you know, how how how this whole thing works, Let's take a look at this prompt. Now, does that mean everything change? No, these are all the same thing. These are all the same thing which are there. Step number one, hidden structure signal. Two distinct elements, sum is the target, you know, that's what we are trying to do in
this case. So, why brute force is not the final solution? And compare the approaches. N square Sorting and binary search, hash set is optimal solution which you can apply And look up look up would be the best way to do that particular solution. Now, we have a Java solution for it which which I can use to really bring that perspective for us. So, that's the way we
can actually understand the problem and then work on it. Okay? Sorted sorted So, that's another thing which we can do. Now, when I'm doing it, you need you need to find the first defective version in sorted sequence of a software release where all versions after that are defective. How would we solve this problem? Approach this problem. The sort this is ordered. Ordered sort is there. So, because
it is ordered sort, we can do binary search. Yes? Which is sometime called as git What is it called as git? bisect. Yeah? Git bisect is exactly that to solve to solve this this particular particular Um okay, great. Now, these are all the problem you can you can run them up, but I let me give you give you the perspective. I want to better understand their breadth
first search and explain the breadth first search and shortest path for that one. So, these problems I would suggest you to run to understand it better how to do how to do these things. So, that's I'm not able to run all of these problems, but this will help you prepare for an interview also and also for your home. I'm not running all of them, so you get
get there get the perspective here. Same thing is to do with a brute force also. You can run the brute force commands and exactly match that for for some of these these things. Now, brilliance also knows trade-offs. Now, you need to do the trade-offs for the engineers and try to look at that. Now, when I say the trade-offs, same command which I ran before, okay? Let me
show you this. Trade-off is very easy, you know, for above. Trade-offs. So, when I am running with a tree of thought process, that's the reason I'm saying tree of thought process. It actually going to go in and look at brute force all three methods and it's going to do a trade-off between all of them and then find out which one has bad scalability. Which one is a
sorting is a better thinking, but the scale stability is not there for that one. Okay, in this case, sorting and two-pointer is better because it works for negatives also. That's a that's a good solution for for us to use. And best solution is scale you know, scalability is more. So, I'm going to pick this one as part of our solution. So, there's a decision tree also created
for this to solve that particular problem. So, does it work for all the problems? Yeah. The same model will work for any lead code problem. So, I'm giving you a formula to run thousands of lead code problem just by doing this exercise. You know, that's the that's the benefit which we get here. So, that's said, what are what is the important thing is optimize is a choice.
You know, that's important patterns. We need to identify these patterns. Now, the main thing is write the beautiful code. The last part is what? Write the beautiful code. So, what does that mean? Production grade code which is AI refinements are there to to solve the problem. So, what changes are there? You move from you know, accepted answer to production ready answer. So, now that's where the prompt
comes in. The prompt which I gave before, you apply that prompt. Now, you are able to solve this particular problem and then say to think as a partner to solve this problem. Okay? So, how do we do that? That's where the creation of production friendly, you know, first of all, explainable modular code comes in. Looking at the weak code, you know, you have to understand where the
problems are. So, this is where we need to spend most amount of time. You know, look at the small methods, you know, understand the edge cases. Now, can I use AI to do this for me? Yeah. Apply the AI principles. Now, we look at the weak code from here and then apply the production lens to it. You know, would you review the standard Can you Can this
be modified without breaking other correctness given to you? And that's where is another thing which you Uh the So, that's where you write the Step number seven is most important, testing. Testing is the last thing we're going to talk about in 5 minutes. Solution is not done for the happy paths only. And that is the reason testing layer is the most important layer. Negative duplicates is there.
Can you use ChatGPT to generate the test data? Yeah, there's no one stopping you to do that. But, you can say that, "Hey, you know, I can use ChatGPT to do that. Regression test also I can run." But, I have master detail relationship. Yeah? Somebody can say, "I have a master order is there. I have order line item. For that order line item, I have products." Yes?
Has anybody had that problem? Everybody got the same problem. Yeah? How would you solve that problem? How would you solve that problem? If I give you a problem like you know, you have an order order has order line item which has product. You have to generate the test data for it. How would you do that? You write SQL statement. Yeah? And and then implement that solution. So,
that is where there are tools available. I'll be discussing that to solve this this perspective for you. So, main thing is high value uses is generate the edge cases. You may have missed. So, what we need to do is generate the edge cases for the two candidates. So, what I can do is I can say I can go back again to our our solution here. Say that,
"You know, >> Generate the edge cases for this problem. Now, already generating the edge cases for the problem which is which is given to me and and you know, trying to come up with that particular answer in this case. Mixed up positive and negative numbers and then applying these pairs to solve this problem. Okay? So, generating the edge Can I use the generate edge cases for me?
Um you know, for different different purposes. Yeah. AI is a really great tool to as a partner to do this edge case generation. Now, because I know this edge case generation is there. Now, can I can I use the AI to write the JUnit test for Yeah, I can use the JUnit test suite. It'll write the write that for me when I'm trying to work on this
solution. So, AI assisted refinement can also be done. Now, this is where we reinstate the ask the optimization solution. So, basically we improve the prompt and then we kind of working on that particular prompt. So, take away for the five is remember that writing the beautiful code is only dependent upon what data is coming in, how the test is coming in for yourself. So, you define the
clear clear intent, separated the concerns, handle the edge cases, explainable code is added, now easy to test and easy to modify. So, this is the six steps which you apply. Once you apply that, then you modularize the design. After the modularization is done, now we are ready to take it to the next level to test the code. Yep. Testing is done with by what? Line by line
reading. We need to do the line by line reading through ChatGPT or by some means. Find out the boundary conditions, special test cases, negative cases, performance thinking, and refinement on the trust. Once you do these things, we are ready to do the testing for it. So, that means bounded, positive, negative, upper bound, lower bound, all these tests we need to apply. And once we do that, we
are able to now take it and test it out. This is just one example on negative test case for a password of a user, username and password, and then we are applying it different different ways of scenarios to use this. Okay? test data creation. I said that I'm going to talk about this, yeah? So, let's talk about test data creation. One of the tool which I like
is Mockaroo. So, Mockaroo, why I like Mockaroo is that if I if I apply with Mockaroo, I will get the prospective Let me show you So, I'm in Mockaroo right now. I can create master detail relationship with this. Let me go to Mockaroo right now. and then within that, Okay, so this is Mockaroo tool which is given. Now, you need to add credit card to this. So,
what you can do is you can go in and go to commerce. It has all other ones here. Go to commerce and say credit card. So, I did credit card now. And the same thing I can also do credit card type. So, I can go to the card and say credit card type. Now, I can generate preview and I can now see all the CSV data or
JSON data. You can get that. You can also do master detail relationship in to solve this prospective. So, there are there are testing tools available in the AI to do these thing. And test harnesses are there to solve So, in the close, the great engineer do not jump to clever answers. They earn That means brute force can you apply brute force to any problem? Yeah, you can
apply the brute force in a interview in production. The real differentiator is not coding for speed. It is structured thinking. So, we are always looking for structured thinking. And then when we do that, clarifying the baseline and applying the brute force on top of it to solve the problem. So, what does that means? Interview in production, the real differentiator is not coding. It is structured thinking and
applying these patterns which we have discussed, seven-step process, and then you can solve these things. Now, these are the links. Why I have these links? So, now you can apply that to to your principal. But, application is done through what? Again, application is done through what? Testing. Yes. So, review. Are we ready for it? Okay. So, we have got you know, filters. We are applying the filters.
We are amazon.com. I'm in the amazon.com and I want to buy a t-shirt. When I'm buying a t-shirt, then I want to see what else can I buy with this t-shirt? And that is where linked data really comes in play. Arts is the same thing. I'm buying a art set. I want to buy some other art set with that. So, data processing, collaborating tools, and social networking
which is there. Question for you. How to find two arrays containing any common item? In this problem. I've got two arrays. And in this case, there's a common item Uh in this case, C is the common item. Yeah, this is true and this one is false. How would you do this? How do you approach this problem? Anyone? >> Doctor It's actually Is it com- Is it comparable
in the first place? Yeah. Okay, yeah, there will be Yeah, I'm I can say that there are only small case A B C D or only these are there no numbers in this, yeah. What else Maximum size of this array will be 26. Because A B C D E F till Z. Only characters for now. Yeah, let's say let's say only that, yeah. >> On ASCII we
will increment uh the counters. Yeah. >> Yeah, that can be there. Now, why I'm talking about all this thing? You guys all passed like you did awesome. So, pat yourself on the back. Because what we are trying to look at is before I go into detail, write down the detail. What are the input? What are the outputs? What are the constraints which are there? What are the
positive constraint? Negative constraint? Why I'm talking about this is because what's the max size of the input coming in? A lot of people talked on this. What's most important? Space versus memory complexity. Now, I can come back and solve this problem through the brute force way and there are multiple ways to do that and I'm not going to go through because it's something we all know. But,
AI does not know. AI need to be told these things and that's the reason negative positive condition. Now, you apply in a prompt create a prompt for it proper prompt for it and then solve for a particular problem. So, sometimes what I do is before solving a problem, I think we got like 1 minute 2 minute. Okay, I I can talk about this, yeah. Uh we have
got Okay, 5:30. Okay. So, so I suggest that you know, look at look at this ask ask from your perspective, you know, how to look at this problem. Basically, we wanted to create something for us to and apply the principles, okay? So, apply the seven-level principles and then that will give you a better understanding. Now, when you get a problem, only I'll just add one one more
thing to it. When you get a problem, what do you need Write down the problem. Apply the prompt which I have given you. There are many prompts are there in in in the directory. You can use those prompt and then say that ask me questions first. Ask ChatGPT to ask you It will get you a better question. Now, you answer those and then you're able to work
on it. So, that's another way to do that. I hope you enjoy this session. You can this practical session you can apply in your projects. Thank you.
More from this event
See all 126 talks →
AI Is Not the Risk. Architectural Drift Is - Sunil Kalkunte
17:39
Breaking the Monolith: Tesco’s Journey to Federated GraphQL with xAPI - Vishwas Chandrashekar
29:13
A Practical Introduction to LangChain4j - Venkat Subramaniam
1:01:28
Beyond the AI Models: How Lowe’s is Building the Store That Knows - Swaroop Shivaram
13:59