General programming problems

  • Often coding interviews involve problems which do not have complicated algorithms or data structures

    • Test ability to work though details and get the edge cases right

    • Practice

1.Check for palindrome

public static boolean isPalindrome(String testString)
{
    testString = testString.toLowerCase();
    
    int index = 0;
    int lastIndex = testString.length() - 1;
    
    while (index < lastIndex)
    {
        char forwardChar = testString.charAt(index);
        char reverseChar = testString.charAt(lastIndex);
        while (forwardChar == ' ')
        {
            index++;
            forwardChar = testString.charAt(index);
        }
        while (reverseChar == ' ')
        {
            index++;
            reverseChar = testString.charAt(lastIndex);
        }
        if (forwardChar != reverseChar)
        {
            return false;
        }
        index++;
        lastIndex--;
    }
    return true;
}

2.Find all points within a certain distance of another point

3.Get the next generation of cell states

  • Given a current generation of cells in a matrix, what does the next generation look like? which cells are alive and which are dead? write code to get the next generation of cells given the current generation

  1. A live cell with fewer than 2 live neighbors dies of loneliness

  2. A dead cell will exactly 2 live neighbors comes alive

  3. A live with greater than 2 live neighbors dies due to overcrowding

4.Break a document into chunks

  1. A chunk can be 5000 or fewer characters in length (This rule is relaxed only under one condition see below)

  2. A chunk should contain only complete paragraphs - This is a hard and fast rule

  3. A paragraphs represented by the ':' character in the document

  4. List of chunks should be in the order in which they appear in the document (do not set them up out of order)

  5. If you encounter a paragraph > 5000 characters that should be in a separate chunk by itself

  6. Get all chunks as close to 5000 characters as possible, subject to the constraints above

  • Suppose the chunk size is 5, rather than 5000 for ease of testing

  • The resultant chunks would be

    • chunk 1: a:bb:

    • chunk 2: cc:

    • chunk 3:abcdef:

    • chunk 4: ab:c:

    • chunk 5: d:

5.Run length encoding and decoding

  • "ABBCCC" will be encoded as "1A2B3C"

  • "AABBBCCCC" will be encoded as "2A3B4C"

  • "1D2E1F" will be decoded as "DEEF"

6.Add two numbers represented by their digits

  • (a + b +餘數) / 10 : 傳到下一位

  • (a + b +餘數) % 10 : 該位的答案

  • 先用動態的容器如list(vector)儲存答案, 最後再回傳array

7.Sudoku validator

  • Given a sudoku board (complete or incomplete) check whether the current state of the board is valid

  • A sudoko board is a 9*9 board which can hold numbers from 1 ~ 9. Any other number on that board is invalid

  • For a sudoku board to be valid

    • 1.No row or column should have numbers 1 ~ 9 repeated

    • 2.No designated 3*3 block within the board should have numbers 1 ~ 9 repeated

8.Increment a number

  • A < B < C < D

    • ABB -> ABC

    • ABBC -> ABBD

    • ABCD -> ABDA

Last updated