Competitive Programming
Ir al canal en Telegram
Solving competitive Programming Questions one day at a time. Group link: https://t.me/daily1interviewprogram Please forward it to your friends
Mostrar másEl país no está especificadoLa categoría no está especificada
4 553
Suscriptores
Sin datos24 horas
Sin datos7 días
Sin datos30 días
Archivo de publicaciones
Day 66 Question:
Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
Difficulty: Medium
=============================
Linux command for the day:
=============================
Linux readelf
Usage:
In Linux, whenever you compile a program and get the object file, it will be in ELF format. “readelf” command is used to read the ELF details of the file. “readelf” and “objdump” makes sense if you are a programmer. It is good to know.
Options:
-a Displays all the information about the file.
-h Displays the header information
-l Display the program headers
-s Display the symbol table of the file.
Example:
For the example, I have written a C program and compiled and got the object file. So we shall see what the command will give the output for different options.
1. readelf –a hello_world.o
2. readelf –h hello_world.o
3. readelf -l hello_world.o
Day 65 Question:
Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
Difficulty: Medium
solution to the previous questions will be provided soon.
=============================
Linux command for the day:
=============================
Linux mv
Usage:
“mv” command is used to move the file from source to destination. It is like cut and paste. It can also be used to rename a file or directory.
Options:
-b Backup the destination file if it exists. The backup file will be appended by “~” symbol.
-f If the destination file is read only, force option will forcefully replace the destination file and delete the original file. If “-f” option is not used, then “mv” command will ask for confirmation before replacing the destination file, if it is read only file.
-i Interactive mode, will prompt before overwriting an existing file.
-n Never overwrite an existing file.
-u Never update an existing file if it is newer. If the existing file is older than the soruce file, then overwrite the destination file.
-v Print the output of the operation.
--help Display help message
--version Display version information.
Example:
1. mv –v source.txt destination.txt
Day 64 Question:
Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
Example 2:
Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
Difficulty: Medium
Companies Asked: Facebook Amazon
Note: The group link is the group description section. Request you to please share it with your firends and in other whatsapp groups. It will help others to get interested in programming.
=============================
Linux command for the day:
=============================
Linux cp
Usage:
“cp” command is used to copy files and directory from one place to another. Copying the files from
Options:
-a Copy the file and retain the metadata of the file as much as possible.
--attributes-only Don’t copy the file, create the file and copy the attributes. If the file already exists then update the attributes.
-f Force copy, if there is a file already exist at the destination, then delete it and copy the file.
-r Copy the directory contents recursively.
-v Verbose mode. Display information after completion of the command.
-u Update, when source is newer than the destination
Example:
1. Simple Copy. “cp source.txt destination.txt”
2. Copy related files. “cp *.txt myDir”.
From the image below, we have copied the files ending with “txt” extension to “myDir” directory.
3. Copy in interactive mode. If the destination file already exists, then it will prompt a message. “cp -i source.txt destination.txt”.
In the image below, as there is already file called as “copied_file.txt”, it will ask for confirmation before replacing the file.
Day 63 Question:
Valid Number
Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Difficulty: Hard
=============================
Linux command for the day:
=============================
Linux rm
Usage:
“rm” command is used to delete files and directories in Linux.
Options:
-f Force remove all the files and sub directories specified.
-i Prompt before deleting a file.
-r Delete directories recursively.
-d remove empty directory
Example:
1. Remove file. “rm hello.txt”
2. Force remove files and directory including sub directories. “rm –fr test_dir”
3. Ask confirmation before deleting any file every time. “rm –i hello.txt”
Day 62 Question:
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Difficulty: Medium
Update with your answers in comment section of below link:
http://bit.ly/q_62
=============================
Linux command for the day:
=============================
Linux touch
Usage:
Touch command is used to create, modify date, time of a file.
Options:
-a Change the access time only.
-c If the file doesn’t exist then, don’t create a new file.
-d update access and modified time
-m change modified time only
-t create file using specified time. [YYDDHHMM]
Example:
1. Create a new file. “touch hello.txt”
2. Change the access time. “touch –a hello.txt”. By using “stat” command we can see the access time and modified time as shown in below image. File was accessed and modified at “12:08”. After running the “touch –a hello.txt”, the access time has been changed to “12:10”
3. Change the access and modified time. “touch –d hello.txt”. Same explanation as above, but additional to changing accessed time, it will also change the modified time.
4. Create a file using specified time [YYDDHHMMte. “touch –t test.txt”t
5. Don’t create new file, if it is not exist. “touch –c test_1.txt”
Solution to Queston 61:
http://bit.ly/q_61
Update with your answers in the comment section of the post above.
Day 61 Question:
The gray code is a binary numeral system where two successive values differ in only one bit.
Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.
Example 1:
Input: 2
Output: [0,1,3,2]
Explanation:
00 - 0
01 - 1
11 - 3
10 - 2
For a given n, a gray code sequence may not be uniquely defined.
For example, [0,2,3,1] is also a valid gray code sequence.
00 - 0
10 - 2
11 - 3
01 - 1
Example 2:
Input: 0
Output: [0]
Explanation: We define the gray code sequence to begin with 0.
A gray code sequence of n has size = 2n, which for n = 0 the size is 20 = 1.
Therefore, for n = 0 the gray code sequence is [0].
Difficulty: Medium
=============================
Linux command for the day:
=============================
Linux file
Usage:
File command is used to determine the type of the file.
Options:
-b Brief mode. It will display only the type of file without the file name.
-i --mime Displays the mime type of the file.
-z Look inside a compressed files.
* Displays the type of all the files.
Example:
1. To know type of file. Use “file ”.
2. To know the file name in brief mode. “file –b ”.
3. To know the mime type of the file. “file –i ”
4. To know the file type inside a compressed file. “file –z ”
5. To know the file types of all the files inside a directory. “file *”.
Day 60 Question:
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
Difficulty: Medium
Update with your answers in the comment section of the link below:
http://bit.ly/q_60
=============================
Linux command for the day:
=============================
Linux rmdir
Name: Remove “empty” Directory in Linux.
Options:
-v Display message after operation is completed.
-p Removes parent directory also.
--ignore-fail-on-non-empty Will not display the error message, if the command is not able to delete the directory because it is not empty.
Example:
1. Remove simple directory. “rm test_dir”
2. Remove directory with exit message. “rm –v test_dir”
3. Remove directory along with parent directory. “rm –p dir_1/dir_2/dir_3”
4. Remove directory by supressing the warnings. “rm --ignore-fail-on-non-empty dir_1”
Day 59 Question:
Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
Difficulty: Medium
Update with your answers in the comment section of the link below:
http://bit.ly/q_59
=============================
Linux command for the day:
=============================
Linux mkdir
Name: Create Linux directory
Options:
-v Write message after every operation
-p Create parent directory if needed
-m To specify the permissions
Example:
1. Create a simple directory. “mkdir test_dir”
2. Create directory with message after operation is completed “mkdir –v test_dir”
3. Create directory with parent directory. “mkdir –p dir_1/dir_2/dir_3”
4. Create directory with permissions. “mkdir –m 777 test_dir”
Hope you have solved Day 58 question. Click the below link to get the solution.
http://bit.ly/q_58
Day 58 Question:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Difficulty: Medium
More details in the below link. Also update with your answer.
http://bit.ly/q_58
=============================
Linux command for the day:
=============================
Linux Absolute and Relative paths
Linux Absolute path: If the path to a particular directory starts from root directory is called as absolute path.
Example:
cd /usr/local/sbin
Linux Relative Path: If the path to a directory starts by taking the present directory as a reference, then it is called as relative path.
Example:
You are in “/usr/local/sbin” and you want to navigate to “/usr/local/”:
Using Absolute path, you write “cd /usr/local/”.
Using Relative path, you write “cd ../”.
Day 57 Question:
Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Difficulty: Medium
Companies Asked: Morgan Stanley, Xome
More details in the below link. Update your queries and answers in the comment section of the post below:
http://bit.ly/q_57
=============================
Linux command for the day:
=============================
Linux cd:
Usage:
“cd” stands for Change Directory.
Change Directory is one of the basic commands to be known. When you are working on a terminal, “cd” is the only command that you can use to navigate from one directory to another. Below we shall see some of the variants of “cd” command.
Options:
-L: Follow the symbolic link.
-P: Resolve the symbolic link, go to the actual directory.
Examples:
1. To go to “/usr/local/” directory from present directory:
cd /usr/local
2. To go to the previous directory from where you were working:
cd –
3. To go to one level up from the present directory use “..”
cd ..
4. To go to home directory use “cd “
Day 56 Question:
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
More details in the below link. Also update with your answer.
http://bit.ly/q_56
Difficulty: Hard
Companies Asked: Accolite, Amazon, Visa, MAQ Software, Amdocs, Twitter
Linux command for the day:
=============================
Linux pwd
Name: Print Working Directory
Description:
This command is used to print the path of present working directory. It will show the path starting from the “root” directory.
Options:
-L – Print the logical directory path, even if it is a symbolic link. This will be the default option, if no options are mentioned.
-P – If the directory is a symbolic link, then resolve that link and print the resolved path.
Hi Dear Members, as communicated we shall resume our competitive programming journey from monday starting from Question No 56.
Hello Dear Members, I have not been active from past 3 days. I am preparing questions to be posted. Hence shall resume the question and answer from next week for sure if not early. I am sorry for the delay.
Day 67 Question:
Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
Difficulty: Hard
Day 66 Question:
Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
Difficulty: Medium
Day 65 Question:
Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
Difficulty: Medium
