PAST QUESTIONS

CodeJam_Feb2020_watermark_blue (1).png

CLICK THE BUTTON BELOW TO BE DIRECTED TO THE HACKERRANK PLATFORM.

There you can attempt our Virtana Code Jam (Feb 2020) questions with similar constraints to our competition. We encourage you to attempt before viewing answers.

This is a great way to practice for future Code Jam competitions.


Round 1 - Q1 - Validate UTF-8 Character

UTF-8 is a character encoding that maps each symbol to one, two, three, or four bytes.

For example, the Euro sign, €, corresponds to the three bytes 11100010 10000010 10101100.

The rules for mapping characters are as follows: For a single-byte character, the first bit must be zero. For an n-byte character, the first byte starts with n ones and a zero. The other n - 1 bytes all start with 10. Visually, this can be represented as follows:

 Bytes   |           Byte format
---------|-------------------------------------
   1     | 0xxxxxxx
   2     | 110xxxxx 10xxxxxx
   3     | 1110xxxx 10xxxxxx 10xxxxxx
   4     | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Problem Statement:

Write a program that takes in a list of integers representing byte values, and returns whether it is a valid UTF-8 encoding.

Input Format:

The first line of input represents the number of bytes, n, in the encoding. The second line represents the n byte values, separated by spaces.

Constraints:

None.

Output Format:

A lowercase string; “true” if the list is a valid UTF-8 encoding, “false” otherwise.

Tags ?

Sample Input:

0 (the Euro sign) 3 226 130 172

+ Solution to Round 1 Question 1

Language: Python

twoESeven = 0b10000000
twoESix   = 0b01000000
twoEFive  = 0b00100000
twoEFour  = 0b00010000
twoEThree = 0b00001000

amt = int(input())
arr = [0]*amt
for i in range(amt):
    arr[i] = int(input())

def validUTF(arr):
    firstByte = arr[0]
    numBytes = 0

    if (firstByte&(twoESeven|twoESix) == twoESeven):
        return "false"
    elif (len(arr) == 1):
        return "true"

    firstByte=firstByte<<1

    while firstByte&twoESeven != 0:
        firstByte = firstByte<<1
        numBytes += 1
        if ((numBytes>=len(arr)) or 
            (arr[numBytes]&(twoESeven|twoESix) != twoESeven) or
            (numBytes >= 4)):
            return "false"

    if ((numBytes+1 != len(arr)) or 
        (numBytes == 0 and len(arr) > 1)):
        return "false"

    return "true"

print(validUTF(arr))

Round 1 - Q2 - Clock Angles

Given a clock time in hh:mm format, determine (rounded up to the nearest degree), the smaller angle between the hour and the minute hands.

Bonus: When, during the course of a day, will the angle be zero?

+ Solution to Round 1 Question 2

Language: Python

import math

def get_angle(hour, minute):
    hour = 0 if hour == 12 else hour

    minute_degrees = minute*6
    hour_degrees = 0.5*(60*hour + minute)

    difference = math.ceil(abs(hour_degrees - minute_degrees))
    acute_angle = min(360 - difference, difference)

    return acute_angle

hour = float(input())
minute = float(input())

# for hour in range(1, 13):
#     for minute in range(0, 60):
#         print(f'{hour}:{minute} = {get_angle(hour, minute)}')

# print(f'{hour}:{minute} = {get_angle(hour, minute)}')

print(get_angle(hour, minute))

Round 2 - Q1 - Find the Nth row of Pascal’s Triangle

Pascal's Triangle is a triangular array on integers constructed with the following formula:

The 0th row consists of the number 1 .
For each subsequent row, each element is the sum of the numbers directly above it, on either side.
For example, here are the first few rows:

      1           Row 0
     1 1          Row 1
    1 2 1         Row 2
   1 3 3 1        Row 3
  1 4 6 4 1       Row 4

Given an unsigned input, k, return the kth row of Pascal's Triangle. You are not required to generate the full triangle.

Bonus: Can you do this using only O(k) space?
Example: k = 5 Output: [1, 5, 10, 10, 5, 1]

+ Solution to Round 2 Question 1

Language C++

#include<iostream>
#include<vector>
using namespace std;

//-------------------Recursive solution-------------------

//Helper function
vector<long long int> generateNextRow(vector<long long int> lastRow) {
    vector<long long int> nextRow;
    //first element
    nextRow.push_back(1);
    //middle elements
    for (int i = 0; i < lastRow.size()-1; i++) {
        nextRow.push_back(lastRow[i] + lastRow[i + 1]);
    }
    //last element
    nextRow.push_back(1);

    return nextRow;
}

vector<long long int> PascalRowRec(int n) {
    vector<long long int> row;
    //base case
    if (n == 0) {
        row.push_back(1); 
        return row;
    }
    //recursive step
    return generateNextRow(PascalRowRec(n - 1));
}

//----------------Iterative solution----------------
vector<long long int> pascalRowIter(long long int n) {
    vector<long long int> generatedRow(n + 1, 0);
    //First element
    generatedRow[0] = 1;
    for (int row = 1; row <= n; row++) {
        for (int j = row; j > 0; j--) {
            generatedRow[j] = generatedRow[j] + generatedRow[j - 1];
        }
    }

    return generatedRow;
}

int main() {
    int rowIndex;
    cin >> rowIndex;
    vector<long long int> rowRec = PascalRowRec(rowIndex);
    vector<long long int> rowIter = pascalRowIter(rowIndex);
    for (int i = 0; i < rowRec.size(); i++)
    {
        cout << rowRec[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < rowIter.size(); i++)
    {
        cout << rowIter[i] << " ";
    }
    return 0;
}

Round 3 - Q1 - Jumping Problem

Starting from 0 on a number line, you would like to make a series of jumps that lead to the integer N.

On the ith jump, you may move exactly i places to the left or right.

Find the fewest number of jumps required to get from 0 to N.

Bonus: Output the path taken.

+ Solution to Round 3 Question 1

Languauge: Python

n = int(input())

def sumTo(x):
    return ((x*(x+1))//2)

def jumping(n):
    n = abs(n)
    x = 0
    while (sumTo(x)<n or (sumTo(x)-n)&1==1):
        x += 1
    print(x)

jumping(n)