Best way to code a shopping cart consisting of items with different properties

This is an interview question. Interviewer wanted a scalable solution

Goal is to find value of shopping cart. Cart could have 3 different types of items(Physical,Digital,URL).

Only Physical Items have a shipping address.

Only Digital items have a redeemable code that is used in a merchant site.

Only URL items have a URL that gives access to some online resource.

I’ve suggested having an Item class with price property which is extended by classes PhysicalItem, DigitalItem, UrlItem. Objects of these classes added to list which is iterated over to find the total order amount. Interviewer wasn’t impressed.

Is there any good design pattern/approach that is best applied here?

Are grammars consisting only of rules with one symbol on each side NL-complete?

The unrestricted grammars characterize the recursively enumerable languages. This is the same as saying that for every unrestricted grammar G there exists some Turing machine capable of recognizing L(G) and vice versa.

Context: Grammars are Turing-complete. Therefore complexity classes like NL have equivalences in grammars.

One important NL-complete problem is ST-connectivity (or “Reachability”) (Papadimitriou 1994 Thrm. 16.2), the problem of determining whether, given a directed graph G and two nodes s and t on that graph, there is a path from s to t. ST-connectivity can be seen to be in NL, because we start at the node s and nondeterministically walk to every other reachable node. ST-connectivity can be seen to be NL-hard by considering the computation state graph of any other NL algorithm, and considering that the other algorithm will accept if and only if there is a (nondetermistic) path from the starting state to an accepting state.

Given a directed graph, deciding if a->b is a directed path is NL-complete.

We will reduce the directed graph to a grammar rules with one symbol on each side:

For each directed edge in the graph, add a grammar rule. The directed edge a->b becomes the grammar rule a|b.

The NL-complete query becomes, “If I set a to the start symbol, can I derive symbol b using the grammar rules?”

Each grammar rule has one symbol on each side (i.e. a|b).

Therefore grammar rules with one symbol on each side is NL-complete.

Are grammars consisting only of rules with one symbol on each side NL-complete?

Find sub-matrix containing the maximum number of elements consisting only of 1’s

I am trying to get help on it here, originally posted first at:

Basically trying to get the max sub matrix that contains only 1’s of a matrix that is filled with 0’s and 1’s.

I could figure out the max sub matrix, now I just need to find how to do so that the sub matrix is only 1’s.

Find sub-matrix containing the maximum number of elements consisting only of 1’s

A matrix of integers of size R x C (R rows, C columns) is given. All its elements have a value of 0 or 1. The rows of the matrix are numbered from 1 to R, the columns are numbered from 1 to C.

I am trying to find the most effective algorithm that will output r1, r2, c1, c2 of a sub matrix that consists only 1’s and creates the maximum number of elements.

So far I thought of making a pre-matrix that counts the sum at each given element in the sub matrix (1, 1, r, c) and store it in a new matrix SUM (with same dimensions as the original matrix).

The formula for this could be then:

SUM[i, j] = SUM[i−1, j] + SUM[i, j−1] − SUM[i−1, i−1] + MATRIX[i, j]

I am not sure if I had to do the sums though, if anyone has experience with a similar problem I would appreciate an explanation.

How do I balance a campaign consisting of four kobold PCs?

I am currently planning a campaign consisting of four kobold PCs, using Volo’s Guide to Monsters (p. 119) as my starting point for the race.

However, I am worried about Pack Tactics and Sunlight Sensitivity.

Pack Tactics. You have advantage on an attack roll against a creature if at least one of your allies is within 5 feet of the creature and the ally isn’t incapacitated.

Sunlight Sensitivity. You have disadvantage on attack rolls and Wisdom (Perception) checks that rely on sight when you, the target of your attack, or whatever you are trying to perceive is in direct sunlight.

Kobolds as pictured in Volo’s Guide seem very strong, gaining advantage on practically every attack roll. The inclusion of Sunlight Sensitivity does not, to me, seem like it’s doing very much to make them weaker – in a typical combat scenario the worst it’ll do is cancel out the advantage given by Pack Tactics, rather than ever realistically give disadvantage. Kobolds obviously favour fighting in the dark, so simply railroading every fight to be in the sunlight does not seem like it would be fun for me nor the players.

How would I bring down the power level of these kobolds to something more resembling that of PHB player races, while hopefully still keeping some kobold flavour?

any arithmetic progression of length 10 consisting of numbers in $S$ will contain at least a number in $P$

Let $ S= \left\{ 1,2,3,…,100 \right\}$ be a set of positive integers from $ 1$ to $ 100$ . Let $ P$ be a subset of $ S$ such that any arithmetic progression of length 10 consisting of numbers in $ S$ will contain at least a number in $ P$ . What is the smallest possible number of elements in $ P$ ?

Denote $ |P|$ as the number of elements in $ P$ . We shall find the smallest possible value of $ |P|$ .

For $ |P|=18$ , choose $ P = \left\{ 10,19,28,37,…,91,12,23,34,…,89 \right\}$ , which consists of all integers from $ S$ that equivalent to $ 1 \pmod 9$ or $ 1 \pmod {11}$ , excluding $ 1$ and $ 100$ . Then every arithmetic progression of length 10 will contain at least a number in $ P$ .

To prove that, let $ a,a+d,a+2d,…a+9d$ be an arithmetic progression of length 10 consisting of numbers in $ S$ with $ 1 \leq d \leq 11$ .

If $ gcd(d,9)=1$ , then there exists $ 0 \leq k \leq 9$ such that $ a+kd \equiv 1 \pmod 9$ . If $ a+kd=1$ or $ 100$ then $ k=0$ or $ 9$ respectively, and thus if $ d<11$ then there exist $ 0 \leq l \leq 9$ such that $ a+ld \equiv 1 \pmod 9$ and $ a+ld \neq 1, 100$ . If $ d=11$ then the arithmetic progression is $ 1,12,23,…,100$ , in which $ 12,23,…,89 \in P$ .

If $ gcd(d,9)>1$ and all elements of $ a,a+d,a+2d,…a+9d$ do not equal to $ 1$ $ \pmod 9$ , then $ d<11$ and thus $ gcd(d,11)=1$ Hence there must be a $ 0 \leq k \leq 9$ such that $ a+kd \equiv 1 \pmod {11}$ . If not, then $ a+10d \equiv 1 \pmod {11} \Leftrightarrow a = d+1$ ; but then $ a \equiv 1 \pmod 3$ , then atleast 3 elements in $ a,a+d,a+2d,…a+9d$ equal to $ 1$ $ \pmod 9$ .

However, for $ |P|<18$ , I can neither find such set $ P$ nor prove that $ |P|$ cannot be less than $ 18$ . So my question is:

Is it true that $ |P| \geq 18$ ? How can I prove it? If not, what is the minimum amount of elements in $ P$ ?

Also, I am wondering that:

If we replace 10 with an even number $ n$ ,and $ 100$ with $ n^2$ , is it true that $ |P| \geq 2(n-1)$ ?

Any answers or comments will be appreciated. If this question should be closed, please let me know. If this forum cannot answer my question, I will delete this question immediately.

In a mixed company consisting of Italian,Turks,German, Chinese and Australian [on hold]

In a mixed company consisting of Italian,Turks, Chinese,German and Australian: 1. The Italian are one third of number of germans,and three less than number of Australian. 2.the Chinese and German together outnumbered the Chinese and Australian by 3. 3.the Chinese and Australian from one less than half of the group,while Chinese and Australian is 7/16 of the company. How many people of each nationality are there?

How can I find the sequence of moves which will end the chess-game(only consisting of pawns)?

The value of (N) for this problem is between [1,100].

There is a chessboard with N rows (numbered 1 through N) and N columns (numbered 1 through N) . Let’s denote a cell in row r and column c of the chessboard by (r,c). Only a few pawns are placed on the chessboard and nothing else.

You may perform any number of moves in any order. In one move, you must take one pawn and capture another pawn with it; just like in standard chess, a pawn in a cell (r,c) may capture only pawns in cells (r−1,c−1) or (r−1,c+1). The captured pawn is removed and the pawn that captured it takes its place. It is not allowed to move pawns without capturing. Unlike in standard chess, the pawns do not have colors, so it is possible to use all pawns to capture and all pawns can be captured.

The number of moves you can make is finite, so the game can always reach a state where you have no valid moves. Find the shortest possible sequence of moves such that there are no more valid moves afterwards.


3X3 chessboard:-

‘O’ denotes pawn and ‘.’ denotes empty cell.

. O .

O . O

. . .


2(moves required):-

2 1 R—>Pawn at (2,1) captures pawn at upper right cell.

2 3 L—>Pawn at (2,3) captures pawn at upper left cell.

After the first move, the chessboard becomes

. O .

. . O

. . .

and after the second move, it becomes

. O .

. . .

. . .

How to prove/verify the following inequality consisting of polynomial and exponential holds?

How to verify the inequation $ C^{H}e^{-\lambda^2 C} \leq \delta$ holds for $ C = \frac{1}{\lambda^{2}} (2H log \frac{H}{\lambda^2} + log\frac{1}{\delta})$ ? Namely, we treat $ H$ , $ \lambda$ , and $ \delta$ as constants and $ C$ as the variable.

Furthermore, can we provide a general condition of C under which the inequation will hold?