Conditional selection between x and y with bitwise operations problem

Problem Given three 32-bit integers x, y, and b, return x if b is 1 and y if b is 0, using only mathematical or bit operations. You can assume b can only be 1 or 0. Solution Method 1 - Using bitwise masks The core idea is to leverage bitwise operations to create masks based on the value of b, which will then be used to select either x or y. ...

Determining the Existence of a Valid Secret Code in Mastermind Based on Given Guesses and Scores

Problem Mastermind is a two-player game in which the first player attempts to guess the secret code of the second. In this version, the code may be any six-digit number with all distinct digits. Each turn the first player guesses some number, and the second player responds by saying how many digits in this number correctly matched their location in the secret code. For example, if the secret code were 123456, then a guess of 175286 would score two, since 1 and 6 were correctly placed. ...

Number of Overlapping Lines between points on 2 parallel lines

Problem Suppose you are given two lists of n points, one list p1, p2, ..., pn on the line y = 0 and the other list q1, q2, ..., qn on the line y = 1. Imagine a set of n line segments connecting each point pi to qi. Write an algorithm to determine how many pairs of the line segments intersect. ...

Fisher-Yates Shuffle

Problem Write a method to shuffle a deck of cards. It must be a perfect shuffle – in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect. OR How can you shuffle an array in O(n) time and O(1) space? For example, if input is an array of (1, 2, 3, 4, 5), one of the output can be (5, 4, 3, 2, 1) or (4, 2, 3, 1, 5). ...

Merge K Sorted Lists

Problem You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Examples 1 -> 10 -> 20 4 -> 11 -> 13 3 -> 8 -> 9 will result in 1 -> 3 -> 4 -> 8 -> 9 -> 10 -> 11 -> 13 -> 20 ...

Paint House 2 - N houses with k colors with no two adjacent houses with same color

Problem There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on… Find the minimum cost to paint all houses. ...

Reconstruct Itinerary Problem

Problem You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it. All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. ...

Rotate n x n matrix by 90 degrees

Problem You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). Another way to ask the same problem Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? Examples Example 1: ...

Square root of an integer Problem

Problem Implement int sqrt(int x). OR Compute and return the square root of x. OR If x is not a perfect square, return floor(sqrt(x)) OR floor(√n) OR Given a non-negative integer x, compute and return the square root of x. Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned. ...

Backspace String Compare Problem

Problem Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. Note that after backspacing an empty text, the text will continue empty. Examples Example 1: Input: s = "ab#c", t = "ad#c" Output: true Explanation: Both s and t become "ac". ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy