Swap two number in place without temporary variables

Problem Write a function to swap two number in place without temporary variables. Solution Method1 - The XOR or Exclusive trick In C this should work: a ^= b ^= a ^= b; to simplify : a=a^b; b=a^b; a=a^b; ...

Integer to English Words Problem

Problem Convert a non-negative integer num to its English words representation. Examples Example 1: Input: num = 123 Output: "One Hundred Twenty Three" Example 2: Input: num = 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: ...

Delete non-tail Node in linked list, Given only access to that Node

Problem Write a function to delete a node in a singly-linked list. You will not be given access to the head of the list, instead you will be given access to the node to be deleted directly. It is guaranteed that the node to be deleted is not a tail node in the list. Examples Example 1: graph LR; 4 --> 5 --> 1 --> 9 style 5 fill:#FF9933 ...

Find if Path Exists in Directed Graph Problem

Problem There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself. You want to determine if there is a valid path that exists from vertex source to vertex destination. ...

Remove duplicate characters in a string

Problem Given a string, Write a program to remove duplicate characters from the string. Examples Example 1: Input: s = kodeknight Output: kodenight Example 2: Input: s = k5kc Output: k5c Solution There are multiple approach to solve it. Method 1 : Brute Force Code C void removeDuplicates(char *str) { if (!str) { return; } int len = strlen(str); if (len < 2){ return; } int tail = 1; for (int i = 1; i < len; ++i) { int j; for (j = 0; j < tail; ++j) if (str[i] == str[j]) { break; } if (j == tail) { str[tail] = str[i]; ++tail; } } str[tail] = '\0'; } ...

Sorting a Stack using push,pop,isEmpty and peek

Problem Sort a Stack using standard operations push, pop, isEmpty and peek. Other ways of asking the question Write a program to sort a stack in ascending order You should not make any assumptions about how the stack is implemented The following are the only functions that should be used to write this program: push | pop | peek | isEmpty Note - peek is equivalent to top() function in java, where you don’t pop out the element but peek and see. ...

April 5, 2022 · 6 min · TagsList of tags for the post  ctci ·  stack

Implement 3 stacks in 1 array

Problem Implement 3 stacks in 1 array Solution Method 1 - Create 3 Stacks with Fixed Max Size Divide the array in three equal parts and allow the individual stack to grow in that limited space (note: [ means inclusive, while ( means exclusive of the end point). for stack 1, we will use [0, n/3) for stack 2, we will use [n/3, 2n/3) for stack 3, we will use [2n/3, n) Code Java int stackSize = 300; int[] buffer = new int[stackSize * 3]; int[] stackPointer = {0,0,0}; // stack pointers to track top element void push(int stackNum, int value) { /* Find the index of the top element in the array + 1, and increment the stack pointer */ int index = stackNum * stackSize + stackPointer[stackNum] + 1; stackPointer[stackNum]++; buffer[index] = value; } int pop(int stackNum) { int index = stackNum * stackSize + stackPointer[stackNum]; stackPointer[stackNum]--; int value = buffer[index]; buffer[index] = 0; return value; } int peek(int stackNum) { int index = stackNum * stackSize + stackPointer[stackNum]; return buffer[index]; } boolean isEmpty(int stackNum) { return stackPointer[stackNum] == stackNum * stackSize; } ...

Count number of bits to be flipped to convert A to B

Problem You are given two numbers A and B. Write a function to determine the number of bits need to be flipped to convert integer A to integer B. OR A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0. For example, for x = 7, the binary representation is 111 and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get 110, flip the second bit from the right to get 101, flip the fifth bit from the right (a leading zero) to get 10111, etc. Given two integers start and goal, return the minimum number of bit flips to convert start to goal. ...

Determine if a string has all unique characters

Problem Implement an algorithm to determine if a string has all unique characters. Solution Method 1 - Brute Force - Compare All Characters Brute force way can be we take each character in the string and compare it to every other character in the string. We do this using for loops. This would mean a time complexity of O(n^2) because of the nested loop. ...

Find the nearest numbers that have same number of 1s for an integer

Problem Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation. Examples Example 1: Input: number = 3 Output: 5 Explanation: Next higher number for 3 is 5. i.e. (00000011 => 00000101). Likewise next lower number of 5 is 3. ...

April 3, 2022 · 3 min · TagsList of tags for the post  ctci ·  bits
This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy