Developer interview questions – Logical thinking problems

By | July 21, 2021

Some logical thinking questions that can be used to test a developers ability to code a solution to a problem. Most of the problems below should be solvable in 45min.

  • An Arnagram

An anagram is a word formed from another by rearranging its letters, using all the original letters exactly once; for example, orchestra can be rearranged into carthorse.
//Write a function which returns all anagrams of a given word (including the word itself) in any order.
//For example GetAllAnagrams(“abba”) should return collection containing “aabb”, “abab”, “abba”, “baab”, “baba”, “bbaa”.

public static ICollection GetAllAnagrams(string str)
{}

  • Binary Search Tree

    Write a function that checks if a given binary tree is a valid binary search tree.A binary search tree(BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node’s left subtree and is smaller than the values in all the nodes in that node’s right subtree.
    For example, for the following tree:

    n1(Value: 1, Left: null, Right: null)
    n2(Value: 2, Left: n1, Right: n3)
    n3(Value: 3, Left: null, Right: null)
    Call to IsValidBST(n2) should return true since a tree with root at n2 is a valid binary search tree.

    Explanation: Subtrees rooted at nodes n1 and n3 are valid binary search trees as they have no children.A tree rooted at node n2 is a valid binary search tree since its value (2) is larger or equal to the largest value in its left subtree(1, rooted at n1) and is smaller than the smallest value in its right subtree(3 rooted at n3).

    public class Node
    {
    public int Value { get; set; }

    public Node Left { get; set; }

    public Node Right { get; set; }

    public Node(int value, Node left, Node right)
    {
    Value = value;
    Left = left;
    Right = right;
    }

    public bool IsValid()
    {}

    }

    public static bool IsValidBST(Node root)
    {}

    static void Main(string[] args)
    {
    Node n1 = new Node(1, null, null);
    Node n3 = new Node(3, null, null);
    Node n2 = new Node(2, n1, n3);

    Console.WriteLine(IsValidBST(n2));

  •  Frog

    A frog only moves forward, but it can move in steps 1 inch long or in jumps 2 inches long. A frog can cover the same distance using different combinations of steps and jumps.
    Write a function that calculates the number of different combinations a frog can use to cover a given distance.
    For example, a distance of 3 inches can be covered in three ways: step-step-step, step-jump, and jump-step.

    public static int count = 0;
    public static IEnumerable parts { get; set; }

    public static int NumberOfWays(int n)
    {}

  • Palindrome

Write a function that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.

For example, IsPalindrome(“Noel sees Leon.”) should return true as spaces, period, and case should be ignored resulting with “noelseesleon” which is a palindrome since it reads same backward and forward.

public static bool IsPalindrome(string str)
{}

  • Index Of Longest Word

Write a function that finds the zero-based index of the longest run in a string. A run is a consecutive sequence of the same character. If there is more than one run with the same length, return the index of the first one.

For example, IndexOfLongestRun(“abbcccddddcccbba”) should return 6 as the longest run is dddd and it first appears on index 6.

public static int IndexOfLongestRun(string str)
{}

  • Path operations

Write a function that provides change directory(cd) function for an abstract file system.

//Notes:
//Root path is ‘/’.
//Path separator is ‘/’.
//Parent directory is addressable as “..”.
//Directory names consist only of English alphabet letters (A-Z and a-z).
//For example, new Path(“/a/b/c/d”).Cd(“../x”).CurrentPath should return “/a/b/c/x”.
//Note: The evaluation environment uses ‘\’ as the path separator.

public string CurrentPath { get; private set; }
public Path Cd(string newPath)
{}

Leave a Reply

Your email address will not be published.