Posted in Arrays and Strings, coding interview preparation, Data Structures/ Leetcode, Leetcode Top 150, Two Pointer Technique

[Problem Solving] Remove duplicates in a sorted array in-place

Problem Statement

Given an array of integers sorted in increasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Return k after placing the final result in the first k slots of nums.

Examples

Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]

This is one of my favorite and easy array problems to start thinking in terms of two pointers to solve array problems. Why two pointers in this case?

We need a pointer that will read the elements of the array one by one. We can use another pointer to overwrite the elements in place. Also whenever we want to modify elements of an array in place – think about whether the two pointer technique can be applied!

The read pointer, let’s say r is pretty straightforward where we increment its value by one during each iteration. The only problem to solve here is to understand when to move the write w pointer.

In this problem, we are going to always compare two elements to each other to find out if it is a repeated value. This naturally leads to maintaining two variables prev and curr to keep track of the previous element to compare the current one with. Now for moving the write pointer,

  • Each time prev = curr, we move the read pointer r and not the write pointer w. This is so that we keep reading the elements until we find the element to overwrite with.
  • When prev != curr, we can now overwrite the array element, increment w and r as well as set prev to curr.

Let’s see how this works using the following diagram. Note that only r and w are variables that we use for the two pointer technique here. Prev and curr are variables to keep track of the previous and current elements.

Complexity

O(N) to traverse the array and O(1) space since we modify the array in-place.

Solution

def removeDuplicates(self, nums: List[int]) -> int:
    if not nums:
        return 0
    if len(nums) == 1:
        return 1
             
    r, w = 1, 1
    prev = nums[0]
    while (r < len(nums) and w < len(nums)):
        curr = nums[r]
        if prev != curr:
            nums[w] = curr
            w += 1
            prev = curr
        r += 1
    return w

Hope you enjoyed learning to solve this array problem with the two pointer technique.

Posted in Health, Professional Improvement

7 Small Habits for Software Engineers

1. Learn a technical concept everyday

This might look impossible when you are constantly juggling multiple things at work. But spend 10 to 20 minutes everyday learning or reading about a new technical concept. To avoid the fatigue of trying to find a topic everyday, you can make a list of 20 to 30 topics beforehand and then go through each item over the course of time.

2. Stand up during meetings

Ideally as a Software Engineer your day should not be filled with meetings. So whenever you get into a meeting, try to take it while standing. If you don’t have your video turned on, feel free to stretch and move around a little during the meeting. Standing desks or standing desk converters that you can place on top of your current ones are a great investment especially at a job that’s done from a desk for 7+ hours everyday.

3. Keep emailing and Slacking at bay

Many Engineers use their precious morning energy and mental clarity to check emails – an activity that mostly does not involve any thinking or creative problem solving except for maybe a couple of important emails everyday. So avoid opening your email first thing in the morning or keeping that tab open all the time. Instead check emails twice a day – maybe before lunch and once before signing off for the day.

Similarly avoid checking Slack/ Team or other messenger notifications all day reactively. You can go one step ahead and keep notifications muted and people can override it in case they really have to get their message across to you. Personally I have kept mine muted for a while now and urgent messages have been pretty rare.

4. Ask at least one question

Being able to ask relevant follow up questions can indicate how much of the content you did grasp from someone’s presentation. Try to think of at least one relevant question to ask during any technical presentation that you attend – be it within or outside the team. When you are listening to a presentation, try to be fully present. Don’t chat or check emails at the same time since our mind has to constantly context switch between different activities.

5. Keep mobile devices far away from your work station

A lot of Engineers keep their phones right by their keyboards and keep checking it every gap they get. We can even lose track of time sometimes in checking phones instead of giving ourselves uninterrupted chunks of time to focus on work. Making sure to focus on one thing at a time can help avoid fatigue and get things done faster. This way you have all the time you need at the end of the day to spend on your phones (although you probably shouldn’t :p)

6. Hydrate and rest your eyes

Software Engineers are required to stare into screens all day which can really cause a lot of damage and fatigue to our eyes. Use a chrome extension that reminds you to take a break from the screen every 20 – 30 mins. During this time you might as well go get some water and walk around!

There are also Pomodoro chrome extensions that nudge you to take a break after completion of focus time intervals.

7. Teach or document what you know

Aim to add slight improvements to existing documentation at least every week or once in two weeks. This will help your team keep documentation up to date.

If you have been working on something for 3+ months, try to teach about it to someone or give a presentation on that topic to your team or even outside your team. There’s nothing that solidifies your knowledge more than trying to teach it to someone.

Hope these tips help you build a better work life as a Software Engineer!

Posted in Health, Professional Improvement

Managing Pandemic Fatigue As a Software Engineer

Remind yourself about why you do what you do

Why are you an Engineer?

If you are feeling a lack of motivation and purpose at work, it’s important to take a step back and understand why you are doing this in the first place. Is it because you love coding, solving problems, managing projects, or working alongside talented people and learning from them? Or maybe you took it up to support yourself or your family financially.

Once you know why you are at this job, try and find out what parts of it are causing the lack of motivation. Perhaps you are stuck at a boring job, or a project that you don’t enjoy or maybe you are working overtime losing track of hours in front of the computer. Have conversations with your manager on what kind of work you would like to do once you have completed the current project and ask if there are certain parts of it that you can delegate to make space for work that interests you. You might be tempted to slog off without communicating but remember that a lack of motivation can kill productivity and the quality of work that you produce which is a lose-lose situation for your career and the company that you work for. If you tried that and you still feel miserable, see if it’s time to look for opportunities outside your company.

Take care of physical and mental health

Take care of Health

Spend a few minutes 3 to 5 days a week to get a quick exercise session in the morning or after work. Establishing a morning/evening routine can help kick start the day in a right way and also make sure that you log off on time after a day’s work. Stretching for few minutes every hour is very helpful to offload the stress from the body from sitting and working in front of a laptop all day. The marinara chrome extension is a super useful tool to schedule focus time with short and long breaks throughout the day.

Have you been waking up and feeling too tired or fatigued to work, more often than before? The pandemic is an enormously difficult time for people around the world. So you are not to be blamed for not feeling up to it. Mental health is as important or in fact more important than physical health. So don’t skimp on taking a couple of sick days off to catch your breath.

If your problems are chronic or are really impacting work, look into ways in which you can get help. You can reach out to your support system or consider therapy to help you navigate the situation. Many companies have been offering various sessions for boosting morale and supporting employees through these difficult times. Try attending them once in a while. Here’s a quick summary of the different ways to care for our health.

  • Exercise for 30 minutes everyday
  • Do mindful meditation for 5 to 10 minutes
  • Stretch every hour or two
  • Eat food that nourishes your brain and body
  • Take time to address mental health issues
  • Spend time outside of work on hobbies
  • Take breaks when you are fatigued
  • Reach out to your social circle
  • Invest in building deeper connections at work

Verbalize the stress

Verbalize the stress

We tend to go through a lot of stress assuming that our colleagues don’t have these problems since they all seem fine externally. It’s mutually helpful to check on your team mates and connect with others around the company who are also dealing with the stressors of the pandemic. This can be a great way for employees to share their stories and exchange helpful tips to move forward through all this. Verbalize your stress by talking about it to others, in coffee chats, or with a licensed therapist. You can also try writing them down, journaling etc which can offload the stress and give you some time to think through it and address them.

Take a vacation

Take a vacation

Many of us might save all the vacation leave not wanting to waste them unless we are on an Instagram worthy vacation. On the contrary taking a shorter/ longer break every few months from work is so important to cope with work life let alone with a pandemic thrown into the mix!

Not taking breaks leads to burnout which can take a huge toll on our health and also impact the quality of work seriously. This becomes a vicious cycle as Engineers tend to lose motivation and purpose when we are not able to produce high quality work.

Find meaning outside of work

Spend time outside of work

For many people, work is a huge part of their identity and this makes them ignore other parts of their lives which can have a significant impact on their well-being. Take time everyday to actively strengthen the aspects of your life beyond work. This could be spending time with your family, friends or neighbors, taking time out for your hobbies and interests, pursuing a side hustle that you are passionate about etc.

Another mistake that a lot of Engineers do is to treat work as their major hobby and don’t take time for anything else in life. This results in overworking beyond work hours which can eventually lead to burnout and a lack of focussed work because increased work hours do not lead to an increase in productivity. In fact, it lowers it significantly. People who take time out to do other things have improved creativity in a problem solving environment.

Have short term goals

Have short term goals

With no definite end in sight, the pandemic can seem like a never ending drag. So focus on short term goals at work to check off every quarter or month. Work with your manager and write these goals down in a shared document so that you have something to work towards in a shorter span of time. Make sure that these goals are reasonable and attainable with a few stretch goals to try and challenge yourself in case the others have been accomplished.

Hope these tips are helpful to you. Above all, be kind to yourself and understand that the world is going through an intense pandemic and we were not prepared to go through the motions of life without being affected by it. Stay safe!

Posted in Professional Improvement

10 Best Practices For Software Engineers To Ensure a Successful Onboarding

1. Version Control Systems

Learning and mastering the basics as well as frequently used version control concepts are crucial for Engineers to make onboarding easier. It also helps to set oneself up for productive future work. If your company uses a graphical UI for source control, there is a temptation to never learn the Command Line Interface (CLI) but you will eventually have to work at companies who extensively use the CLI. If you are going to be using the Git Version Control System, then here’s my article on some of the Git commands that I have used the most in my career. This should get you through 90% of your Git encounters.

2. Befriend Your IDEs

As soon as you meet your team, find out about IDE(s) they prefer to use. If there’s a clear majority, spend some time learning the basics of using it, including but not limited to cloning repositories, automatic deployment and syncing code changes with a remote host, debugging workflows, automatic refactoring – For example, IntelliJ offers a way to refactor a constructor and replace it with with either a Factory or Builder pattern in Java which can come very handy. Learn shortcuts to search for files, keywords, classes, functions etc. in any code base.

3. Searching For Information

Some companies do a great job of documenting their code, processes and knowledge base and some don’t. Wherever you are, it helps to understand how to search for documentation, previous conversations or issues around a certain topic, code search, ticket search if you’re using a tool like JIRA to manage tasks. The ability to know where and how to search for information facilitates a natural curiosity to indulge in problem solving time before deciding to give up too early and reach out for help. Eventually you might enjoy helping out other Engineers as well with their questions which solidifies the knowledge gained.

4. Regular 1:1s With Manager/ Mentors

One aspect of my current job that I enjoy the most are the 1:1s I have with my manager, mentors and mentees every week. These are some ideas to help you get started with 1:1s with your manager.

  • How did you feel about last week’s work? How was your weekend?
  • What did you most enjoy working on?
  • What exhausted you and is there some help that you need?
  • Feedback on team members/ projects
  • Any areas where you struggle with or need more information on
  • Suggestions for team improvements
  • Any feedback you have for your manager

An important thing to remember here is to be candid in your 1:1s which will make it easier for others to help or guide you in the right direction. If you are bored working on something, voice it!

5. Do Code Reviews

Find opportunities to do code reviews for your team members. If you are super new to the team, ask to be added to the code reviews of repositories that you will soon be working with. This is a great way to understand what kind of changes are pushed and how the technical material that you’re reading links to actual code. It also exposes the coding standards to adhere to within your team. If you are already familiar with the coding language used by your team, this is a great opportunity to warm up to new code while helping the team with the expertise that you are bringing in.

6. Mix It Up

Onboarding can get boring and overwhelming when you spend all your day reading up on documentation. Try to have a good mix of concrete coding tasks as well as something to read and absorb since you will have to look up all that information later anyway. Take regular breaks and make notes of terms that you want to look up later. Get a breath of information before deciding to dive deeper into a particular topic. If things get too difficult to comprehend, setup a 1:1 with a team member and ask for a quick walkthrough.

7. Get To Know The Team

Setting up 1:1s with all your team mates in the first few weeks of joining a new team will bring a sense of familiarity and comfort as a new Engineer. It’s also the time to discover the expertise and interests of people in the team and you may even find a great mentor.

Another aspect of knowing the team is to learn what you will actually be doing. Think of questions such as this.

  • Who are your customers?
  • What products do you build and own?
  • Where can you find a list of repositories owned by the team?
  • Any important code that you can take a look at?
  • How does the team contribute to the company’s growth?
  • How is your team adding value to other teams across the company?
  • Get a team member to subject expertise mapping from your manager to understand who would be the best person to help you with certain topics

8. Set Clear Objectives

Having a clear set of objectives makes sure that onboarding does not feel like a never ending process. Your team probably owns a lot of code and processes but it’s important to not get overwhelmed by it and instead focus on a small subset of core topics and tickets to work on. If the ramp up is draining or overwhelming, do reach out to your manager. When you work outside of office hours or withhold from surfacing the challenges that you are facing, you miss out on giving valuable feedback on the onboarding process. This could potentially help out future members joining the team. As you check off items on your ramp up, make sure to keep notes on what’s working and what isn’t, any questions that you have and links that you want to read up on later. This is a great way to build your knowledge base.

9. Look Outside Your Team

One of the first and foremost things that I did when I joined my current company Yelp was to scan for organizations or employee groups that I’d love to connect with outside of my team. For more than a year now, I have absolutely enjoyed participating in various events and programs for Women in Engineering. 1:1s are a great place to express your areas of interests and colleagues almost always remember to point you to some amazing groups that you can be a part of. These are also great opportunities to grow as an Engineer and a leader outside of regular coding tasks. Get to know about some interesting communication channels or emails you can subscribe to.

10. Wrap Up Onboarding The Right Way

  • Make sure to offer feedback on the onboarding process and mentorship that you have received so far. Highlight the work that interested you most and any challenges that you faced with existing processes, codebases or systems.
  • Find ways to improve existing code, documentation or processes however small it might be.
  • Talk to your manager about areas that you felt aligned better with your interests and see if there are existing projects that you can be a part of.
  • If you think you got the most out of your mentorship, feel free to space out your future sessions or be on a lookout for new mentors in your areas of interest.

A well laid out onboarding process is when you feel slightly uncomfortable and challenged in good ways but not overwhelmed to a point of exhaustion. That’s the sweet spot to find – not too easy, not impossible either like the Goldilocks Effect on Infants! Hope you enjoyed reading this post. If you have more ideas on how to improve onboarding, feel free to leave your comments on this post. Have a great week!

Posted in coding interview preparation, Professional Improvement

The different types of interviewers whom you may have to work with.

The future co-worker

This is an interviewer who can put themselves in your shoes the minute they walk-in. They understand what an important day this is for you and the nervousness you carry. They know that you have done incredible amounts of hard work and won’t try to belittle you in any way. They are here because they want to solve the problem with you. As you go through your interview, whether you aced it or not, you will come out of it feeling like you had a good conversation where you learnt something. This is the kind of interviewer who is great to work with during and after the interview. Although you might think that you are the only one being evaluated, it is a great opportunity for you to evaluate whether you can see yourself working with this person in the future.

Now that we’ve gotten the best package, let’s look at some of the not-so-good scenarios that can happen.

The one who wouldn’t look at you

Sometimes interviewers have been told to type every little conversation that goes on in the room. Although there are some great note-takers who aren’t constantly distracted, most of them in this category don’t care to look at you much. The typing is distracting, there is no eye contact, mostly they aren’t listening nor trying to have an organic conversation with you.

If you had to interview with someone like that, don’t be afraid to take up space in the room. If you start feeling stressed or the interview feels too cold, politely let them know that the typing is a little distracting to your thought process. You can also pause a few times and wait to get their attention before proceeding so that they don’t miss out on the key points that you are mentioning during your interview. It is important for you to stay assured that you are not responsible for their distracted state no matter how boring the question is, so go about it confidently because this day is more important to you than it is for them.

The one who wants you to be a mind-reader

Some interviewers do not let you solve a problem using your own methods. This could be because they aren’t aware of more than one way of solving a problem or they aren’t able to understand how you’re trying to solve it.

Let us always assume the latter because if you are standing there judging the interviewer for not knowing multiple ways of solving a problem, it won’t benefit you in any way. This can create a communication barrier between the both of you. This is when you can take a step back and try to best explain your algorithm with a few examples. Place emphasis on why your approach might work. Pay attention when the interviewer counters it. In some cases, they might expect you to go for the most efficient solution right away. You have probably only come up with a brute force one and can’t really think of how it can be improved. When you feel stuck in this state, explain that you haven’t quite gotten there but you are hoping to get started with a basic solution and iteratively improve upon it once you see the caveats. Most interviewers will be fine with this approach.

The one with the unnecessary arrogance

An interviewer’s job is to make a case for someone who exhibits potential to be a good developer and fit for the company and its culture. Unfortunately, some of them try to check if you are as smart as them, or pick a super difficult problem. This might mostly be a junior developer whose idea of Software Development hasn’t grown beyond solving difficult Leetcode questions yet.

Such an interview scenario can be extremely stressful and the worst outcome is you experiencing a mental block. Try to pick a few easy example cases and manually solve the problem. See if you can turn that into a brute force algorithm that can work for the most common cases. You can then discuss edge cases and improve your algorithm. At any point, an interviewer should not try to trick you or turn it into a stress interview. If that starts to happen, take a few deep breaths and start over. Remember that it’s okay to sometimes not solve it at all. There are thousands of different problems out there and it’s completely okay to be clueless when an overly difficult problem gets presented to you. It was just one of those unlucky days.

The silence of the lambs

This type of interviewer can make you feel nervous throughout the interview by being silent. You may feel stuck at various points on whether your approach is right, waiting for cues to proceed or step back. A good interviewer will make sure to guide you and let you know when you are going in the wrong direction, but sometimes they might not. If this happens, calm yourselves and give it your best shot. Start explaining how you plan on solving the problem and once you are done, stop and explicitly ask whether you can go ahead and code it up. Get to this point as quickly as possible so that you have enough time to brainstorm an alternate approach if your first one doesn’t work out. Be aware of time and allocate a comfortable chunk of time to code the solution.

Hope this post helped you to mentally prepare yourselves to interview with any type of interviewer whom you might encounter. Good luck with your upcoming interviews 🙂