Software Engineer Interviews - Partition Even and Odd Elements

 

Problem

  • Given an array of numbers, write a program such that:
    • Move even numbers to the left of the array
    • Move odd numbers to the right of the array

Example:

  • Inputs: a[] = [5,8,7,2,6,1,9,4,3,10]
  • Expected Output: [10,8,2,4,6,1,7,9,3,5]

Process

  1. Understand the problem
  2. Brainstorm Possible Scenarios
  3. Brainstorm Possible Approaches
  4. Write Code for a selected approach
  5. Write Tests
  6. Walkthrough

Step 1: Understand the Problem

Ask questions:

  • Is additional data structure allowed?
  • Should the original sequence of numbers should be maintained?
  • Are duplicate values are allowed in the array?

Document the Problem:

  • Given an array of numbers, move even numbers to the left and odd numbers to the right
  • No additional data structures allowed, needs to be done in-place
  • No Original sequence of numbers doesn’t have to be maintained
  • Duplicate values are possible in the array

Step 2: Brainstorm Possible Scenarios

Scenarios

  • Happy Case
    • Input: a[] = [5,8,2,4,6,1,9,3,10]
    • Expected output...
Continue Reading...

Software Engineer Interviews - Check Pairs for a given Sum in an Array

 

Problem

  • You are given 2 inputs: an array of numbers and a number
  • Let’s call the input array as a[] and input number as target
  • Write a program to check if 2 elements exist in a[] such that their sum add up to target
  • For example, if the input array is [4,5,1,3,2,7,9] and target value is 16, the program should return true, since 7 and 9 sum up to be 16

Process

I like to follow the following process / framework when I do interviews

  1. Understand the problem
  2. Brainstorm Possible Scenarios
  3. Brainstorm Possible Approaches
  4. Write Code for a selected approach
  5. Write Tests
  6. Walkthrough

 Part 1: Understand the problem + Brainstorm Scenarios  

 

Part 2: Brainstorm Approaches + Brute Force Approach

 

Step 3: Hash Table Approach

 

Part 4: Implementing the Brute Force Approach + Tests

 

Part 5: Implementing the Hash Table Approach + Tests 

 

Notes 

Continue Reading...
Close

Releasing Soon!

I'm finishing last few iterations on my courses. Sign up below to be notified when they're released.