The interview guide

Junior dev - Coding interview Questions & Answers — Part 1

Rajai Kumar
Nerd For Tech
Published in
3 min readFeb 4, 2021

--

Computer photo created by pressfoto — www.freepik.com

Coding challenges was one of the biggest fears in my junior dev days. I was an average programmer. But once I knew the mechanics behind them, it became one of my favourite pass times.

If you feel you are weak in an area in Software Development and you want to get better at it, there is only one way to go, Practice! Practice! Practice!

If I don’t practice for some time I’ll feel weak too. I’ll pick it up in a day or two after I’ve started practice again.

I’ve compiled some questions I faced and some of the questions I asked. The choice of programming language for this series would be C++. The reason I didn’t use swift as usual was most interviewers like to see us use raw techniques rather than using in-build functions. And who doesn’t love C++, it’s a legend.

We are going to cover four simple challenges.

  1. Factorial
  2. Palindrome
  3. Maximum sum of an hour glass
  4. Find the biggest and smallest number in the array.

Factorial

Question:Write a function which takes a positive integer from user and calculates the factorial of that number. Use recursion to solve this.

Recursion-

The repeated application of a recursive procedure or definition.

Consider input = n. Then we need

n*(n-1)*(n-2)*(n-3)* ….3*2*1

Example: n=4 then its

4*(4–1)*(4–2)*(4–3)

So the factorial for 4 is 24.

But we need to solve this using recursion. So the formula is

n! or factorial of n = n*(n-1)!

n! = 1 if n = 0 or n = 1

All we need is if n is greater than 1 then we need n* factorial of n else return 1

#include<iostream>
using namespace std;

int factorial(int n);//Declaring our factorial function

int main()
{
int n;

cout << "Enter a positive integer: ";
cin >> n;

cout << "Factorial of " << n << " = " << factorial(n); //The call

return 0;
}
//Defining our factorial function
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}

Palindrome

A string is said to be a palindrome if the reverse of the string is same as string.

Question: Write a function to check if a given string is a palindrome or not.
For example, “Nayan” is palindrome, but “Trisha” is not palindrome.

Interviewers rarely ask to use recursion to solve this. But they would like it, trust me.

Let’s see how to solve this.

Consider we are sending a word as an input, then we need the index of the first and last element of the word. We will use this to compare whether they are equal from front to back. We have to increment the start index variable and decrement the end index variable until the start index variable is larger or equal to the end index variable.

For example, if the word is Peep then

start = 0, end = 3

Run the recursion until the start is larger than or equal to the end.

++start & --end
  1. First element and last element of Peep are same. -Now start =1 & end = 2
  2. Second element and third element of Peep are same. -Now start =2 & end = 1

Now the check succeeds, and we have a palindrome in hand.

When comparing elements if any of them are not equal, we return a false

#include <iostream>using namespace::std;// Palindrome functionbool isPalindrome(const string &input, int start, long end)
{
if (start>=end)return true;if (input[end]!=input[start])return false;return isPalindrome(input, ++start, --end);}//Main functionint main(int argc, const char * argv[]) {string input;cout<<"Enter a string to check whether it's palindrome or not: ";cin>>input;cout<<"Is "<<input<<" a Palindrome, say 1 for true and 0 for false: \n Answer is "<<isPalindrome(input, 0, input.length()-1)<<"\n";return 0;}

That’s it guys, time for me to leave.

Rest will be covered in part 2. And I’m planning to run this as a series. So look forward to it.

Please find the code in this repo -

Note: For people who don’t use Xcode, just use the “main.cpp” file.

--

--

Rajai Kumar
Nerd For Tech

I’m a workaholic iOS developer. I focus a lot on UX and product efficiancy.