Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [programming-challenge]

Programming challenges are off-site challenges meant to offer programmers educational experiences while testing their abilities.

2
votes
2answers
40 views

square the elements of a sorted list and give the output in sorted order

Given a sorted list of integers, square the elements and give the output in sorted order. For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81]. My solution 1: ...
10
votes
2answers
600 views

Given the mapping a = 1, b = 2, … z = 26, and an encoded message, count the number of ways it can be decoded

Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded. For example, the message '111' would give 3, since it could be decoded as 'aaa',...
5
votes
1answer
173 views

Counting primes in a range whose digits are all prime

Codewars Kata: Given integers a and b, count all primes p where a ≤ p < b and the digits of p are all prime. b may be as large as 107. To start with, I hardcoded all valid 1903 primes in \$[1, 10^...
4
votes
3answers
45 views

Print out N by N Spiral Matrix in javascript

I got this question during my practice interview. Matrices - N Spiral Matrix Prompt Given an integer N, output an N x N spiral matrix with integers 1 through N. Examples: ...
2
votes
1answer
25 views

Craps Simulator Exercise

I did another exercise, this time it simulates n games of craps(dice game) and outputs the wins and win percentage! ...
3
votes
1answer
25 views

LRUCache for integers using dict + linkedlist

From leet code question: https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and ...
2
votes
1answer
79 views

First missing positive integer in linear time and constant space

This problem was asked by Stripe. Given an array of integers, find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that ...
3
votes
1answer
41 views

React.js Nested Nav Bar

This was written for a coding challenge for a company I recently starting working for. I'm looking for any suggestions on how to clean up the code, as well as any issues anyone thinks may occur as it ...
4
votes
1answer
137 views

Regex to find variants of “Google”

From the HackerRank question definition: The word google can be spelled in many different ways. E.g. google, g00gle, g0oGle, g<>0gl3, googl3, GooGIe etc... Because g = G o = ...
4
votes
2answers
91 views

Leetcode 38: The “count-and-say” sequence

38. Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following: ...
8
votes
2answers
780 views

Concatenate words in such a way as to obtain the longest possible sub-string of the same letter

An array of N words is given. Each word consists of small letters ('a'- 'z'). Our goal is to concatenate the words in such a way as to obtain a single word with the longest possible sub-string ...
1
vote
2answers
39 views

Work out largest prime factor of a number

I am trying to get better at problem solving, so I am going through challenges on Project Euler. Problem: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of ...
3
votes
1answer
38 views

Hamming Distance in Python

I was solving this Leetcode challenge about Hamming Distance. Would love feedback on my syntax and code style. Here's the challenge description: The Hamming distance between two integers is the ...
5
votes
1answer
40 views

Codewars: N smallest elements in original order

I am consistently timing out on the mentioned kata: Task You will be given an array of random integers and a number n. You have to extract n smallest integers out of it preserving the original ...
1
vote
3answers
42 views

SPOJ Adding reversed number in Java

I just started to practice for competitive programming and just started to solve the most solved problems on SPOJ. This is what I made for ADDREV: Adding reversed numbers in Java (https://www.spoj....
5
votes
1answer
59 views

Linear regression on points specified by the user in a GUI

Here's my take on the Linear Regression exercise from John Zelle's Python book. Open a window, let the user click on as many points as they want. Upon pressing the "Done" button the regression line ...
3
votes
1answer
54 views

Leetcode 102: Binary tree level-order traversal in Swift

102. Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary ...
3
votes
3answers
94 views

LeetCode Problem 66 : Plus One

I have started practicing problems in leetcode and I solved the following problem on LeetCode. The challenge is : Given a non-empty array of digits representing a non-negative integer, plus one to ...
-2
votes
0answers
22 views

Generalized Hailstone that converges with the sequences [closed]

Can someone please help me solve this problem in python? Thank you in advance! ...
1
vote
1answer
36 views

Leetcode 3Sum problem using hashmap in Python

For practice, I solved Leetcode 15. 3Sum question: Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives ...
3
votes
2answers
81 views

Find the longest sub string of a word after concatenation of given words array

An Array of N words is given. Each word consists of small letters ('a'-'z'). Our goal is to concatenate the words in such a way as to obtain a single word with longest possible substring composed of ...
4
votes
3answers
277 views

Collect subsequences of consecutive integers

I've written a function to split a list into several lists with consecutive integers, e.g.: extract_subsequences([1,2,3,7,8,9]) ...
3
votes
1answer
78 views

Word break problem with recursion and memorization [closed]

For practice, I solved Leetcode 139. worbreak question: Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-...
5
votes
1answer
233 views

Count pairs in list of integers such that their addition is equal to the input value

Given a List<int>, the problem I am trying to solve is: Find the number of unique pairs in List<int> such that their ...
2
votes
2answers
164 views

Mr. Muffin's ball-passing game

I have an assignment to solve this problem. There are a total of 18 test case files, 5 of which I have failed due to exceeding some time limit on the script and I am unable to verify if there are any ...
0
votes
2answers
74 views

Leetcode: Merging 2 sorted lists

https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. ...
9
votes
4answers
1k views

Compute binary XOR of all integers in a range, mod 2

here I am taking 3 inputs. 1st input will take test cases and the other two input will take starting and ending range. the program is running fine as expected but the limit of this question for ...
21
votes
3answers
4k views

Leetcode: Valid parentheses

https://leetcode.com/problems/valid-parentheses/ Given a string containing just the characters (, ), ...
2
votes
1answer
72 views

Leetcode Three Sum in Python

Here's my solution for the Leet Code's Three Sum problem -- would love feedback on (1) code efficiency and (2) style/formatting. This is Python 3. Problem: Given an array nums of n integers, are ...
1
vote
2answers
125 views

Faster code for leetcode reverse int

This is my solution for the leetcode reverse int question: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 ...
0
votes
0answers
65 views

Time limit exceeded on HackerRank “Username Disparity”

Here is the hackerrank question: For two strings A and B, we define the similarity of the strings to be the length of the longest prefix common to both strings. For example, the similarity of ...
2
votes
2answers
102 views

Project Euler problem 86 taking a long time

I'm trying to solve problem 86 in Project Euler. After some tinkering, I managed to unroll the DP solution into a loop. But still the solution takes >150s to complete. What can I do to improve the ...
2
votes
5answers
167 views

Largest binary gap

I took a task from Codility to find longest sequence of zeros in binary representation of an integer. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. ...
3
votes
1answer
60 views

Python Inheritance and Class (Hackerank problem)

I just deep dived to Python and I came with this Hackerrank problem: Create two class, Person with firstName, ...
3
votes
5answers
472 views

Finding Isogram Word

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times. The ...
8
votes
4answers
2k views

Reversing a string - two approaches

Test Case: Input- "I'm hungry!" Output- "!yrgnuh m'I" Approach 1: In this approach, I used a empty string and bind it with the input string reversely. ...
3
votes
0answers
53 views

Kattis Ants challenge

I am trying to learn Haskell doing some contest problems. I have worked a bit with the language but still have a very long way ahead. Right now I am working with a problem called Ants: An army of ...
3
votes
0answers
70 views

Given an input string and a pattern, implement wildcard pattern matching

I have been trying to solve "Wildcard Matching" on leetcode. I know there are more "manual" ways to solve this without using the RE library. But I would like to know if passing the time limit is ...
1
vote
1answer
40 views

DM::OJ Slot Machines challenge in Python 3

I have written a solution to the Slot Machines challenge on DM::OJ: Martha takes a jar of quarters to the casino with the intention of becoming rich. She plays three machines in turn. Unknown to ...
2
votes
2answers
75 views

The next prime number in Python

I have written a solution to a DM::OJ exercise to find the next Prime Number after a given number (input). I'm seeking to optimize it so that it will run for any number up to ...
1
vote
3answers
83 views

Kattis prime reduction challenge

I am solving the Prime Reduction challenge on Kattis. Consider the following process, which we’ll call prime reduction. Given an input x: if x is prime, print x and stop factor x into its ...
3
votes
3answers
133 views

Find MD5-hash whose hexadecimals starts like digits of π

I saw a programming puzzle. Find a string containing letters from a to z such that its MD5-hash starts like 314159265359. I am a junior programmer so I would like to know if I can solve the problem ...
0
votes
2answers
49 views

Euler Project #3 Largest prime factor timeout

I have been recently trying my hands on Euler's Project through HackerRank and got stuck with project #3 where you have to find the Largest Prime Factor. Question: The prime factors of 13195 are 5, 7,...
3
votes
1answer
54 views

Leetcode Mountain Array

I've been working on longest mountain in array on Leetcode: Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: ...
4
votes
4answers
127 views

Python solution to The Hardest Logic Puzzle Ever

The The Hardest Logic Puzzle Ever recently came to my attention: Three gods A, B, and C are called, in no particular order, True, False, and Random. True always speaks truly, False always ...
1
vote
3answers
97 views

Remove duplicates from sorted array, in place

I solved this problem: Given a sorted array nums, remove the duplicates in-place such that each element appears only once and return the new length. Do not allocate extra space for another array, ...
5
votes
2answers
97 views

Advent of Code 2018 Day 4 - Pythonic Sleepy Guards

Introduction I'm using Advent of Code 2018 to learn Python better, interested in the new type support for Python 3.7, I decided to go with that. Here is my solution to Advent of Code Day 4, both ...
5
votes
2answers
215 views

Size of the largest connected component in a grid in Python

Given an R x C grid of 1s and 0s (or True and False values), I need a function that can find the size of the largest connected ...
2
votes
1answer
77 views

String reversing x times

Inspired by CodeWars We have a string s Let's say you start with this: "String" The first thing you do is reverse it: "gnirtS" Then you will take ...
2
votes
1answer
132 views

LeetCode #28 - Boyer Moore string matching algorithm

The following code implements the Boyer Moore string matching algorithm. The algorithm is used to find a sub-string (called the pattern) in a string (called the text) and return the index of the ...